【发布时间】:2020-07-03 02:43:47
【问题描述】:
我有一个按钮,按下它会在 2 秒内运行 CABasicAnimation。我第一次按下按钮时,动画运行良好,图层被填充为红色。我第二次按下按钮动画不再运行。红色层消失了,因为我删除了它,但它并没有从头开始。
在下面的代码中,basicAnimation 是一个类属性。我还尝试在 buttonPressed 方法本身中创建它,这没有任何区别,然后我尝试将其添加为可选的var basicAnimation: CABasicAnimation?,在 buttonPressed 方法中对其进行实例化,然后在 animationDidStop(_:finished:) 委托方法中将其设置为 nil,但均未成功。
为什么不再运行?
let shapeLayer = CAShapeLayer()
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
// var basicAnimation: CABasicAnimation? // I tried this
override func viewDidLoad() {
super.viewDidLoad()
let circularPath = UIBezierPath(arcCenter: view.center, radius: 100, startAngle: -.pi/2, endAngle: 3 * .pi/2, clockwise: true)
shapeLayer.path = circularPath.cgPath
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineWidth = 10
shapeLayer.strokeEnd = 0
view.layer.addSublayer(shapeLayer)
}
@objc func buttonTapped() {
// basicAnimation = CABasicAnimation(keyPath: "strokeEnd") // I tried this
// let basicAnimation = CABasicAnimation(keyPath: "strokeEnd") // I also tried this
removeAnimation()
basicAnimation.delegate = self
basicAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.default)
basicAnimation.fromValue = 0
basicAnimation.toValue = 1
basicAnimation.duration = CFTimeInterval(2)
basicAnimation.fillMode = CAMediaTimingFillMode.forwards
basicAnimation.isRemovedOnCompletion = false
shapeLayer.add(basicAnimation, forKey: "myAnimation")
}
func removeAnimation() {
shapeLayer.removeAnimation(forKey: "myAnimation")
}
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
print("animation done") // prints once
// basicAnimation = nil // I tried this
}
【问题讨论】:
标签: ios swift cabasicanimation