用法1 Value方式
一个数组,提供了一组关键帧的值, 当使用path的 时候 values的值自动被忽略。
//创建动画对象
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//设置value
NSValue *value1=[NSValuevalueWithCGPoint:CGPointMake(100, 100)];
NSValue *value2=[NSValuevalueWithCGPoint:CGPointMake(200, 100)];
NSValue *value3=[NSValuevalueWithCGPoint:CGPointMake(200, 200)];
NSValue *value4=[NSValuevalueWithCGPoint:CGPointMake(100, 200)];
NSValue *value5=[NSValuevalueWithCGPoint:CGPointMake(100, 300)];
NSValue *value6=[NSValuevalueWithCGPoint:CGPointMake(200, 400)];
animation.values=@[value1,value2,value3,value4,value5,value6];
//重复次数 默认为1
animation.repeatCount=MAXFLOAT;
//设置是否原路返回默认为NO
animation.autoreverses = YES;
//设置移动速度,越小越快
animation.duration = 4.0f;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.timingFunction=[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.delegate=self;
//给这个view加上动画效果
[moveView.layer addAnimation:animation forKey:nil];
####### 用法2、 Path方式
这是一个 CGPathRef 对象,默认是空的,当我们创建好CAKeyframeAnimation的实例的时候,可以通过制定一个自己定义的path来让 某一个物体按照这个路径进行动画。这个值默认是nil 当其被设定的时候 values 这个属性就被覆盖
//创建动画对象
CAKeyframeAnimation *animation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
//创建一个CGPathRef对象,就是动画的路线
CGMutablePathRef path = CGPathCreateMutable();
//自动沿着弧度移动
CGPathAddEllipseInRect(path, NULL, CGRectMake(150, 200, 200, 100));
//设置开始位置
CGPathMoveToPoint(path,NULL,100,100);
//沿着直线移动
CGPathAddLineToPoint(path,NULL, 200, 100);
CGPathAddLineToPoint(path,NULL, 200, 200);
CGPathAddLineToPoint(path,NULL, 100, 200);
CGPathAddLineToPoint(path,NULL, 100, 300);
CGPathAddLineToPoint(path,NULL, 200, 400);
//沿着曲线移动
CGPathAddCurveToPoint(path,NULL,50.0,275.0,150.0,275.0,70.0,120.0);
CGPathAddCurveToPoint(path,NULL,150.0,275.0,250.0,275.0,90.0,120.0);
CGPathAddCurveToPoint(path,NULL,250.0,275.0,350.0,275.0,110.0,120.0);
CGPathAddCurveToPoint(path,NULL,350.0,275.0,450.0,275.0,130.0,120.0);
** animation.path=path;**
CGPathRelease(path);
animation.autoreverses = YES;
animation.repeatCount=MAXFLOAT;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.duration = 4.0f;
animation.timingFunction=[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.delegate=self;
//调用
** [moveView.layer addAnimation:animation forKey:nil];**