我用两个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