How to reuse an CABasicAnimation when not removed after completion?

走远了吗. 提交于 2020-01-03 09:02:22

问题


Lots of people are talking about keeping an CABasicAnimation object around after it has been used.

So by setting

removedOnCompletion = NO

the animation object keeps attached to the layer when the animation is complete. How would I re-launch this animation again without creating a new CABasicAnimation?

What's the point of keeping this object around? The only benefit I know is that we can set removedOnCompletion = NO and set kCAFillModeForwards so that Core Animation does not revert the visual representation back to the model values in the CALayer.

But how to re-use the animation, like everyone is talking about?


回答1:


Try this:

  1. Add the animation with a key using [layer addAnimation:myAnimation forKey:@"myKey"]

  2. To get a reference to it later, call [layer animationForKey:@"myKey"]

  3. Read this note from Apple which explains how to pause and resume (restart) animations.




回答2:


This worked for me but I am sure someone will tell me the value is already stored in a key..

When I created a layer and assigned an animation to it I set the animation to a keyed value associated with the layer.

in this example layersCurrentAnimation is the CABasicAnimation and myAnimatedLayer is a CALayer.

[myAnimatedLayer setValue:layersCurrentAnimation forKey:@"basicAnimation"]

then if the animation was to stop I would get the animation value out from the layer and re-run it

CABasicAnimation *thisAnimation = [myAnimatedLayer  valueForKey:@"basicAnimation"];

[myAnimatedLayer addAnimation:thisAnimation forKey:nil];


来源:https://stackoverflow.com/questions/4758085/how-to-reuse-an-cabasicanimation-when-not-removed-after-completion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!