CAAnimation动画的一些知识点
分隐式动画和显式动画 CAAnimatione采用了CAMediaTiming协议,可以调整时间,包括持续时间,速度,重复次数;采用了CAAction协议,可以通过响应动作的方式来显示动画. CAAnimation的一些派生类: CATransition 提供渐变效果:(推拉push效果,消退fade效果,揭开reveal效果) CAAnimationGroup 允许多个动画同时播放 CABasicAnimation 提供了对单一动画的实现 CAKeyframeAnimation 关键桢动画,可以定义行动路线 CAConstraint 约束类,在布局 器类中用它来设置属性 CAConstraintLayoutManager 约束布局管理器,是用来将多个CALayer进行布局的.各个CALayer是通过名称来区分,而布局属性是通过CAConstraint来设置的. CATransaction 事务类,可以对多个layer的属性同时进行修改.它分隐式事务,和显式事务. 事务管理(Transactions) 事务分两种: 1.隐式事务(implicit transaction) 除显式事务外,任何对于CALayer属性的修改,都是隐式事务.这样的事务会在run-loop中被提交. 如: theLayer.opacity = 0.0; theLayer.zPosition = -200; theLayer.position = CGPointMake(0.0, 0.0); 2.显式事务(explicit transaction) a. 通过明确的调用begin,commit来提交动画.优点是可以同时修改多个Layer的属性. 如: [CATransaction begin]; [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions]; [aLayer removeFromSuperlayer]; [CATransaction commit]; b.可以重置持续时间 可以在begin,commit对中临时修改动画持续时间. [CATransaction begin] [CATransaction setValue:[NSNumber numberWithFloat:10.0f] forKey:kCATransactionAnimationDuration]; theLayer.zPosition = 200.0; theLayer.opacity = 0.0; [CATransaction commit]; c.事务可以嵌套. 如: //第一层嵌套 [CATransaction begin]; [CATransaction setValue:[NSNumber numberWithFloat:2.0f] forKey:kCATransactionAnimationDuration]; theLayer.position = CGPointMake(0.0, 0.0); //第二层嵌套 [CATransaction begin]; [CATransaction setValue:[NSNumber numberWithFloat:5.0f] forKey:kCATransactionAnimationDuration]; theLayer.zPosition = 200.0; theLayer.opacity = 0.0; [CATransaction commit]; [CATransaction commit]; 布局管理器示例如下: //创建和设置一个布局管理器 theLayer.layoutManager = [CAConstraintLayoutManager layoutManager]; //创建layerA CALayer *layerA = [CALayer layer]; layerA.name = @"layerA"; //设置layerA的中点位置等于超类的中点位置 [layerA addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidY relativeTo:@"superLayer" attribute:kCAConstraintMidY]]; [layerA addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidX relativeTo:@"superLayer" attribute:kCAConstraintMidX]]; [theLayer addSublayer:layerA]; //创建layerB CALayer *layerB = [CALayer layer]; layerB.name = @"layerB"; //设置layerB的宽度等于layerA的宽度 [layerA addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintWidth relativeTo:@"LayerA" attribute:kCAConstraintWidth]]; [layerA addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidX relativeTo:@"layerA" attribute:kCAConstraintMidX]]; [theLayer addSublayer:layerB];