After rotating a CALayer using CABasicAnimation the layer jumps back to it's unrotated position

泪湿孤枕 提交于 2019-11-28 16:59:31

Have you set the removedOnCompletion property of the rotation animation to NO, e.g.,

rota.removedOnCompletion = NO;

That should leave the presentation layer where it was when the animation finished. The default is YES, which will snap back to the model value, i.e., the behavior you describe.

The fillMode should also be set, i.e.,

rota.fillMode = kCAFillModeForwards;

I was trying to rotate an arrow back and forth, like the Twitter/Facebook "Pull to Refresh" effect.

The problem is, I was doing the rotation back and forth on the same UIView, so after adding

rotation.removedOnCompletion = NO;
rotation.fillMode = kCAFillModeForwards;

The forward animation war working OK but the backwards animation was not working at all.

So I added the last line suggested by yeahdixon, and in addition set the view's transform to the animation's completed state: (rotation by 180 degrees)

[myview.layer removeAllAnimations];
myView.transform = CGAffineTransformMakeRotation(M_PI);

For the 'restore' animation (backwards) I do this on completion:

myView.transform = CGAffineTransformMakeRotation(0);

...and it works fine. Somehow it doesn't need the removeAllAnimations call.

I found that by setting : removedOnCompletion = NO;

did not produce a visible leak in instruments, but did not get deallocated and was accumulating a small amount of memory. IDK if its my implementation or what, but by adding removeAllAnimations at the end of the animation seemed to clear out this tiny bit of residual memory.

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