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 会跳回原始位置。有什么办法可以防止这种情况发生吗?任何帮助表示赞赏。

4

1 回答 1

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.);
于 2012-09-11T04:36:54.957 回答