[英]CATransaction completion block being called immediately when animation is added to subview
我正在嘗試在新添加的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
}
知道為什么它不起作用嗎?
顯然,動畫不在渲染樹上。 即視圖沒有superView
。 或其superView
沒有superView
地方。 您可能需要檢查實現視圖的功能是否已添加到window
或某處的view
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.