Я пытаюсь запустить анимацию на недавно добавленном UIImageView как подпредставление, но кажется, что в этом случае блок завершения вызывается немедленно, даже если анимации добавляются после CATransaction.setCompletionBlock
addSubview(imageView)
CATransaction.begin()
let posAnimation = positionAnimation(startPoint: startPoint, endPoint: endPoint, beginTime: beginTime, duration: duration)
let alpAnimation = alphaAnimation(beginTime: beginTime, duration: duration)
CATransaction.setCompletionBlock{ [weak self] in
print("deleting view")
//imageView.removeFromSuperview()
}
imageView.layer.add(posAnimation, forKey: nil)
imageView.layer.add(alpAnimation, forKey: nil)
CATransaction.commit()
Анимация:
private func positionAnimation(startPoint: CGPoint, endPoint:CGPoint, beginTime: CFTimeInterval, duration: CFTimeInterval) -> CAKeyframeAnimation {
let positionAnimation = CAKeyframeAnimation(keyPath: "position")
positionAnimation.path = customPath(middlePoint: startPoint, endPoint: endPoint).cgPath
positionAnimation.isRemovedOnCompletion = false
positionAnimation.duration = duration
positionAnimation.beginTime = beginTime
positionAnimation.fillMode = CAMediaTimingFillMode.forwards
return positionAnimation
}
private func alphaAnimation(beginTime: CFTimeInterval, duration: CFTimeInterval) -> CAKeyframeAnimation {
let alphaAnimation = CAKeyframeAnimation(keyPath: "opacity")
alphaAnimation.isRemovedOnCompletion = false
alphaAnimation.fillMode = CAMediaTimingFillMode.forwards
alphaAnimation.values = [1.0, 1.0, 0.0]
alphaAnimation.keyTimes = [0.0,0.5,1.0]
alphaAnimation.duration = duration
alphaAnimation.beginTime = beginTime
return alphaAnimation
}
Есть идеи, почему это не работает?