动画CAShapeLayer:完成后做



我有一个CAShapeLayer,它的strokeEnd我偶尔在NSTimer循环内更新。工作得很好,当我调用:

myPathLayer.strokeEnd += 0.1

这个路径改变会自动按照我想要的样子动起来。我的问题是,因为这不是一个函数调用,没有一个完成块。我想在动画完成后执行另一个动作。我怎样才能做到这一点呢?

你可以用cattransaction来为图层动画设置一个完成块。

CATransaction.begin()
CATransaction.setCompletionBlock({self.myFunction()})
myPathLayer.strokeEnd += 0.1
CATransaction.commit()

希望有更好的方法,但我就是这样做的。

您可以使用CAAnimation委托方法。

class Whatever: CAAnimationDelegate { ...
然后

let animation = CABasicAnimation(keyPath: <your_key_path>)
...
animation.delegate = self

并使用委托方法:

func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
     if flag { <do_what_you_need_to> }
}

最新更新