总结:
在图层上可以添加动画,隐式动画不可以停止,显式动画可以停止、移除。在显式动画中停止动画通过删除动画来实现,移除的过程中会有一个跳动的效果,可以通过检测动画最后的状态值,然后赋给layer tree中相关的对象。如果有很多动画要在一个layer上执行,可以把所有的动画通过group(CAAnimationGroup)的形式整合到一起。并且动画的执行状态是可以检测,有两种方法:一种是block的形式,一种是代理的形式。如果两个动画之间的执行顺序是相互制约的可以通过beginTime属性使一个动画在另一个动画执行完成之后执行。在UIView中,如果想要添加动画不能直接添加,因为UIView类并没有提供这些方法,必须把动画添加到UIView的block中才能生效,在这个block之外添加的动画都是不起作用的。
[UIView animateWithDuration:1.0 animations:^{
// Change the opacity implicitly.
myView.layer.opacity = 0.0;
// Change the position explicitly.
CABasicAnimation* theAnim = [CABasicAnimation animationWithKeyPath:@"position"];
theAnim.fromValue = [NSValue valueWithCGPoint:myView.layer.position];
theAnim.toValue = [NSValue valueWithCGPoint:myNewPosition];
theAnim.duration = 3.0;
[myView.layer addAnimation:theAnim forKey:@"AnimateFrame"];
}];