CATranscation使用点

375 阅读1分钟

CATranscation在隐式动画中会被系统在runloop中自动调用一次。

隐式动画默认在layer的相关动画属性改变时发生。

如果是view相关属性发生变化,因为view实现了layer的代理方法actionforkey:,返回了nil,所以不会有隐式动画,可以通过两个方式解决,1.UIViewAnimation相关方法;2.继承UIView,负载-actionForLayer:forKey:方法;3.创建显示动画

[CATranscation begin]与[CATranscation commit]之间的动画设置语句只对其语句之后设置的属性起作用;如果有嵌套,内部被嵌套的动画在外层commit方法之后才会执行。

在动画过程中,layer的presentationLayer属性返回呈现图层,呈现图层记录更实时的layer属性,模型图层记录动画结束时属性的值,在动画过程中同步动画和处理用户交互的时候使用presentationLayer非常合适,其modelLayer属性可以获取他所以来的模型图层。

CALayer中存储动画的配置数据,如果通过key-CAAction键值对的方式,key是具有隐式动画的属性字符串,指定该属性隐式动画的方式,当对应属性发生改变时,隐式动画会通过CAAction的方式进行过度,CAAnimation即遵守CAAction协议。

layer.actions = @{@"backgroundColor":transition};

如果通过addAnimation:<#(nonnull CAAnimation *)#> forKey:<#(nullable NSString *)#>的方式添加动画,则是一次性的动画,因为其Key是动画标识,不是属性名称。 

CABasicAnimation设置removedOnCompletion和fillMode属性后,对象之执行完动画之后保留结束的状态,但是图层的属性值保持原始值

imgAni.removedOnCompletion = NO;imgAni.fillMode=kCAFillModeForwards;

CAKeyframeAnimation *animaiton = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];  
NSArray *rotationVelues = @[@(M_PI_4), @(-M_PI_4), @(M_PI_4)];  
animaiton.values = rotationVelues;  
animation.rotationMode = kCAAnimationRotateAuto;  //方向
animaiton.duration = 3.0f;  
animation.keyTimes = @[@0.2 ,@0.8 ,@1];
animation.path = bezierPath.CGPath;
animaiton.repeatCount = HUGE_VALF;     //   #define    HUGE_VALF    1e50f  
[self.imageView.layer addAnimation:animaiton forKey:nil];