我用兩個CAShapeLayer's
做了一個簡單的進度線。動畫正常工作,但從未調用CAAnimationDelegate
animationDidStart
和animationDidStop
。CAAnimationDelegate代表永遠不會被調用
這裏是我的代碼:
class ProgressBar: UIView, CAAnimationDelegate {
var bottomProgressBar = CAShapeLayer()
var topProgressBar = CAShapeLayer()
let animation = CABasicAnimation(keyPath: "strokeEnd")
override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
}
func configure() {
animation.delegate = self
// setup the two layers
}
func animate(toValue: Double) {
animation.duration = 1.0
animation.fromValue = 0
animation.toValue = toValue
topProgressBar.strokeEnd = CGFloat(toValue)
topProgressBar.animation(forKey: "animate")
}
func animationDidStart(_ anim: CAAnimation) {
print("start")
}
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
print("stop")
}
}
animate
方法被多次調用值的範圍從0到1
有誰知道爲什麼這些代表們從來沒有叫什麼名字?
您是否曾將動畫添加到「CALayer」? – nathan
'topProgressBar.animation(forKey:「animate」)'應該是'topProgressBar.add(animation,forKey:「animate」)'? – nathan
我不會添加它。我是不是該 ?因爲對我來說這段代碼有效。但是現在我想看看動畫什麼時候開始和結束,這部分不起作用。 – Kobe