用法1? Value方式
一個(gè)數(shù)組,提供了一組關(guān)鍵幀的值, 當(dāng)使用path的 時(shí)候 values的值自動(dòng)被忽略。
//創(chuàng)建動(dòng)畫對(duì)象
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//設(shè)置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];
//重復(fù)次數(shù) 默認(rèn)為1
animation.repeatCount=MAXFLOAT;
//設(shè)置是否原路返回默認(rèn)為NO
animation.autoreverses = YES;
//設(shè)置移動(dòng)速度忘衍,越小越快
animation.duration = 4.0f;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.timingFunction=[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.delegate=self;
//給這個(gè)view加上動(dòng)畫效果
[moveView.layer addAnimation:animation forKey:nil];
####### 用法2?逾苫、 Path方式?
這是一個(gè) CGPathRef 對(duì)象,默認(rèn)是空的枚钓,當(dāng)我們創(chuàng)建好CAKeyframeAnimation的實(shí)例的時(shí)候隶垮,可以通過制定一個(gè)自己定義的path來讓 某一個(gè)物體按照這個(gè)路徑進(jìn)行動(dòng)畫。這個(gè)值默認(rèn)是nil 當(dāng)其被設(shè)定的時(shí)候 values 這個(gè)屬性就被覆蓋
//創(chuàng)建動(dòng)畫對(duì)象
CAKeyframeAnimation *animation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
//創(chuàng)建一個(gè)CGPathRef對(duì)象秘噪,就是動(dòng)畫的路線
CGMutablePathRef path = CGPathCreateMutable();
//自動(dòng)沿著弧度移動(dòng)
CGPathAddEllipseInRect(path, NULL, CGRectMake(150, 200, 200, 100));
//設(shè)置開始位置
CGPathMoveToPoint(path,NULL,100,100);
//沿著直線移動(dòng)
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);
//沿著曲線移動(dòng)
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;
//調(diào)用
** [moveView.layer addAnimation:animation forKey:nil];**