是CApropertyAnimation的子类,跟CABasicAnimation的区别是:CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CAKeyframeAnimation会使用一个NSArray保存这些数值,就可以实现CALayer的某一属性按照一串的数值进行动画,就好像制作动画的时候一帧一帧的制作一样。
几个关键属性:
- values:就是上述的NSArray对象。里面的元素称为”关键帧”(keyframe)。动画对象会在指定的时间(duration)内,依次显示values数组中的每一个关键帧
- path:设置一个CGPathRef\CGMutablePathRef的路径对象,默认nil,让CALayer跟着路径移动。path只对CALayer的anchorPoint(锚点)和position(位置)起作用。如果你设置了path,那么values将被忽略
- keyTimes:可以为对应的关键帧指定对应的时间点,其取值范围为0到1.0,keyTimes中的每一个时间值都对应values中的每一帧.当keyTimes没有设置的时候,各个关键帧的时间是平分的
说明:CABasicAnimation可看做是最多只有2个关键帧的CAKeyframeAnimation
- Values方式
// 创建5个关键帧的点对象
CGPoint p1 = CGPointMake(, );
CGPoint p2 = CGPointMake(, );
CGPoint p3 = CGPointMake(, );
CGPoint p4 = CGPointMake(, );
CGPoint p5 = CGPointMake(, ); // 将创建的关键帧放入数组中,用NSValue结构体类型转化成对象
NSArray *values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:p1], [NSValue valueWithCGPoint:p2], [NSValue valueWithCGPoint:p3], [NSValue valueWithCGPoint:p4], [NSValue valueWithCGPoint:p5], nil]; CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; // 给Values属性赋值
[animation setValues:values]; // 动画完成后保持最新状态
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards; animation.duration = 4.0; // 动画定时函数属性
animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; animation.delegate=self; [self.myView.layer addAnimation:animation forKey:nil];
- 第23行:timingFunction 用于变化起点和终点之间的插值计算,形象点说它决定了动画运行的节奏
- kCAMediaTimingFunctionLinear 线性,即匀速
- kCAMediaTimingFunctionEaseIn 先慢后快
- kCAMediaTimingFunctionEaseOut 先快后慢
- kCAMediaTimingFunctionEaseInEaseOut 先慢后快再慢
- kCAMediaTimingFunctionDefault 实际效果是动画中间比较快.
- 注意:当上面的预置不能满足你的需求的时候,你可以使用下面的两个方法来自定义你的timingFunction
* + (id)functionWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;
* - (id)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;
- Path方式:
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; CGMutablePathRef myPath = CGPathCreateMutable(); // 将路径的起点定位到(50, 120)
CGPathMoveToPoint(myPath, NULL, 50.0, 120.0); // 添加5条直线的路径到myPath中
CGPathAddLineToPoint(myPath, NULL, , );
CGPathAddLineToPoint(myPath, NULL, , );
CGPathAddLineToPoint(myPath, NULL, , );
CGPathAddLineToPoint(myPath, NULL, , );
CGPathAddLineToPoint(myPath, NULL, , ); // 添加4条曲线路径到myPath中
CGPathAddCurveToPoint(myPath,NULL,50.0,275.0,150.0,275.0,70.0,120.0);
CGPathAddCurveToPoint(myPath,NULL,150.0,275.0,250.0,275.0,90.0,120.0);
CGPathAddCurveToPoint(myPath,NULL,250.0,275.0,350.0,275.0,110.0,120.0);
CGPathAddCurveToPoint(myPath,NULL,350.0,275.0,450.0,275.0,130.0,120.0); //以"position"为关键字创建 实例
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; // 是否动画回到原位
[animation setAutoreverses:YES]; // 设置path属性
animation.path = myPath; // 释放路径
CGPathRelease(myPath); animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards; animation.duration = 4.0; animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; animation.delegate=self; [self.myView.layer addAnimation:animation forKey:nil];
- 其他属性
- calculationMode 这个属性用来设定 关键帧中间的值是怎么被计算的