(一)基于Layer属性的动画

更改Layer的属性更改可引起动画,如上图所示列出了常用的可动画属性。全部的属性有:anchorPoint、backgroundColor、backgroundFilters、borderColor、borderWidth、bounds、compositingFilter、contents、contentsRect、corderRadius、doubleSided、filters、hidden、mask、masksToBounds、opacity、postion、shadowColor、shadowOffset、shadowOpacity、shadowPath、shadowRadius、sublayers、sublayerTransform、transform、zPosition。
其中更改不同的属性可引起的动画类型不同,有CABasicAnimation(duration是0.25秒或者当前动画事务的duration,key path就是layer的属性名),还有CATransition(duration是0.25秒或当前动画事务的duration,Type是kCATransitionFade,起始progress是0.0,终止progress是1.0)。
(二)Layer对象有其自己的坐标系,同时使用基于点的坐标系和unit坐标系,他们有不同的作用。


(三)Layer可以在三维上操作,layer的transform和sublayerTransform属性

(四)在IOS中,UIView的默认layer的CALayer类实例。如果你想更改它,就就重载UIView的layerClass方法,并返回你自定义的Layer类。系统提供的CALayer子类有:CAEmitterLayer、CAGradientLayer、CAEAGLLayer、CAReplicatorLayer、CAScrollLayer、CAShapeLayer、CATextLayer、CATiledLayer、CATransformLayer。
(五)Layer的内容提供方法:
1、设置layer.contents=[CGImageRef ...];和layer.contentsGravity=kCAGravityCenter...;layer.contentsScale=...;适用于静态的或很少更改的layer。
2、layer.delegate=...;指定layer的delegate,让delegate绘制layer的内容。
3、定义一个layer子类,并自己绘制layer的内容。
(五)Layer有其自己的Background和Border

layer.backgourndColor、borderColor、borderWidth、corderRadius
layer.masksToBounds属性为yes时,corderRadius会影响到contents的显示,否则不影响。
(六)Layer支持内建的shadow
layer shadows的opacity默认为0,同样的masksToBounds属性为yes时,shadow的变化会影响contents的显示。
二、CABasicAnimation
CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnim.fromValue = [NSNumber numberWithFloat:1.0];
fadeAnim.toValue = [NSNumber numberWithFloat:0.0];
fadeAnim.duration = 1.0;
[theLayer addAnimation:fadeAnim forKey:@"opacity"];
// Change the actual data value in the layer to the final value.
theLayer.opacity = 0.0;
三、CAKeyframeAnimation
// Animation group
CAAnimationGroup* group = [CAAnimationGroup animation];
group.animations = [NSArray arrayWithObjects:colorAnim, widthAnim, nil];
group.duration = 5.0;
[myLayer addAnimation:group forKey:@"BorderChanges"];
六、动画结束时得到通知:
1、[animation setCompletionBlock:]
2、[animation setDelegate:]通过代理方法animationDidStop:finished:实现。
3、如果你想连接2个动画,那就不要使用通知了,使用[animation setBeginTime:]实现就可以了。
七、在IOS中修改UIView的Layer动画,必须在UIView的animation block中执行。UIView类默认禁用了layer动画,但在animation block中又重新启用了layer动画。
八、Layer的某系属性会影响到subLayer,例如speed属性,默认是1,如果设置成2,那么动画时subLayer的动画速度会加快一倍,并且时间缩短一半。
九、调整Layout属性:使用一个或多个CAConstraint对象。


// Create and set a constraint layout manager for the parent layer.
theLayer.layoutManager=[CAConstraintLayoutManager layoutManager];
// Create the first sublayer.
CALayer *layerA = [CALayer layer];
layerA.name = @"layerA";
layerA.bounds = CGRectMake(0.0,0.0,100.0,25.0);
layerA.borderWidth = 2.0;
// Keep layerA centered by pinning its midpoint to its parent's midpoint.
[layerA addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidY relativeTo:@"superlayer" attribute:kCAConstraintMidY]];
[layerA addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidX relativeTo:@"superlayer" attribute:kCAConstraintMidX]];
[theLayer addSublayer:layerA];
// Create the second sublayer
CALayer *layerB = [CALayer layer];
layerB.name = @"layerB";
layerB.borderWidth = 2.0;
// Make the width of layerB match the width of layerA.
[layerB addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintWidth relativeTo:@"layerA" attribute:kCAConstraintWidth]];
// Make the horizontal midpoint of layerB match that of layerA
[layerB addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidX relativeTo:@"layerA" attribute:kCAConstraintMidX]];
// Position the top edge of layerB 10 points from the bottom edge of layerA.
[layerB addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMaxY relativeTo:@"layerA" attribute:kCAConstraintMinY offset:-10.0]];
// Position the bottom edge of layerB 10 points
// from the bottom edge of the parent layer.
[layerB addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMinY relativeTo:@"superlayer" attribute:kCAConstraintMinY offset:+10.0]];
[theLayer addSublayer:layerB];
十、还可以使用delegate的layoutSubLayersOfLayer:来手动实现布局。
如果你有自己的Layer子类,你可以重载layoutSublayers方法来处理任何的layout任务。
十一、不像UIView,父Layer不自动clip子Layer的内容。通过设置masksToBounds属性为YES来修改此行为。
十二、转换坐标:convertPoint:fromLayer:、convertPoint:toLayer:、convertRect...、convertTime...
十三、CATransition
CATransition* transition = [CATransition animation];
transition.startProgress = 0;
transition.endProgress = 1.0;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
transition.duration = 1.0;
// Add the transition animation to both layers
[myView1.layer addAnimation:transition forKey:@"transition"];
[myView2.layer addAnimation:transition forKey:@"transition"];
// Finally, change the visibility of the layers.
myView1.hidden = YES;
myView2.hidden = NO;
十四、显式Transaction
[CATransaction begin]; // Outer transaction
// Change the animation duration to two seconds
[CATransaction setValue:[NSNumber numberWithFloat:2.0f] forKey:kCATransactionAnimationDuration];
// Move the layer to a new position
theLayer.position = CGPointMake(0.0,0.0);
[CATransaction begin]; // Inner transaction
// Change the animation duration to five seconds
[CATransaction setValue:[NSNumber numberWithFloat:5.0f] forKey:kCATransactionAnimationDuration];
// Change the zPosition and opacity
theLayer.zPosition=200.0;
theLayer.opacity=0.0;
[CATransaction commit]; // Inner transaction
[CATransaction commit]; // Outer transaction