我有一个CAShapeLayer
添加到了UITableViewCell
。对该层进行动画处理后,animationKeys()
中的动画关键点在动画完成后不会消失。但是,我已经在正常的UIView
中测试了该层,完成动画后,animationKeys()
应该是空的。在UITableViewCell
中添加图层时,这有何不同?
我访问animationKey()
的原因是要检查动画是否还在制作动画。
UITableViewCell
除了CAShapeLayer
之外别无其他。 UIViewController
和UITableView
都没有。
动画代码:
var clockwise = true
@objc func buttonPressed(_ sender: UIButton) {
let animation = CABasicAnimation(keyPath: "transform.rotation")
if let presentation = customLayer.presentation() {
animation.fromValue = presentation.value(forKeyPath: animation.keyPath!)
}
print(customLayer.animationKeys() ?? "its nil!") // first time its "nil", but prints `transform.rotation` after that.
let radian = customLayer.animationKeys()?.isEmpty ?? true ? CGFloat(360) / 180.0 * .pi * (clockwise ? 1 : -1) : 0
let result = CATransform3DMakeRotation(radian, 0, 0, -1)
animation.toValue = radian
animation.duration = 2.5
animation.isRemovedOnCompletion = true
animation.fillMode = kCAFillModeRemoved
let curve = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
animation.timingFunction = curve
CATransaction.begin()
CATransaction.setDisableActions(true)
customLayer.transform = result
CATransaction.commit()
customLayer.add(animation, forKey: animation.keyPath)
clockwise = !clockwise
}