在单元格中时不会删除图层动画

时间:2018-08-27 07:59:12

标签: swift animation cashapelayer

我有一个CAShapeLayer添加到了UITableViewCell。对该层进行动画处理后,animationKeys()中的动画关键点在动画完成后不会消失。但是,我已经在正常的UIView中测试了该层,完成动画后,animationKeys()应该是空的。在UITableViewCell中添加图层时,这有何不同?

我访问animationKey()的原因是要检查动画是否还在制作动画。

UITableViewCell除了CAShapeLayer之外别无其他。 UIViewControllerUITableView都没有。


动画代码:

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
}

0 个答案:

没有答案