2012-08-06 66 views
0
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.33], [NSNumber numberWithFloat:0.5], [NSNumber numberWithFloat:1.0], nil]; 
[anim setKeyTimes:times]; 
NSArray *values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint: CGPointMake(100., 100.)], [NSValue valueWithCGPoint: CGPointMake(100., 200.)], [NSValue valueWithCGPoint: CGPointMake(220., 200.)], [NSValue valueWithCGPoint: CGPointMake(220., 100.)], nil]; 
anim.speed=1.0f; 
[anim setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]; 
[anim setValues:values]; 
[anim setDuration:6.0]; 
[sublayer addAnimation:anim forKey:@"position"]; 

我能夠在CALayer跳回到原始位置後,讓CALayer在路徑上進行動畫處理。有什麼辦法可以防止這種情況發生?任何幫助表示讚賞。使卡拉萊不會移動到原始位置

回答

2

添加動畫後,需要將圖層的位置設置爲動畫的最終位置。

CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.33], [NSNumber numberWithFloat:0.5], [NSNumber numberWithFloat:1.0], nil]; 
[anim setKeyTimes:times]; 
NSArray *values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint: CGPointMake(100., 100.)], [NSValue valueWithCGPoint: CGPointMake(100., 200.)], [NSValue valueWithCGPoint: CGPointMake(220., 200.)], [NSValue valueWithCGPoint: CGPointMake(220., 100.)], nil]; 
anim.speed=1.0f; 
[anim setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]; 
[anim setValues:values]; 
[anim setDuration:6.0]; 
[sublayer addAnimation:anim forKey:@"position"]; 
[sublayer.layer.position = [[values lastObject] CGPointValue];// this will set the position to the final position of the animation, (220., 100.); 
相關問題