CAAnimation: Don't reset after animation

*爱你&永不变心* 提交于 2019-12-12 03:04:06

问题


I have this code to move a view.

CABasicAnimation *theAnimation; 
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
theAnimation.duration=1;
theAnimation.repeatCount=1;
theAnimation.autoreverses=NO;
theAnimation.fromValue=[NSNumber numberWithFloat:0];
theAnimation.toValue=[NSNumber numberWithFloat:-60];
[view.layer addAnimation:theAnimation forKey:@"animateLayer"];

The problem is: The actual coordinates of the view get reseted after the animation has finished. Is it possible that the view remains at the new coordinates after the animation has finished?

Thanks.


回答1:


In addition to attaching an animation that animates the property, you have to actually set the property to its final value. Crazy, isn't it? Try doing this before you add the animation:

[view.layer setValue:[NSNumber numberWithFloat:-60] forKey:@"transform.translation.x"];

I strongly recommend watching the WWDC 2011 video "Core Animation Essentials". It explains this, and covers a lot of useful information for anyone using Core Animation. You can find it here: https://developer.apple.com/videos/wwdc/2011/



来源:https://stackoverflow.com/questions/8856706/caanimation-dont-reset-after-animation

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