让calayer不要移动到原来的位置

时间:2012-08-06 17:13:28

标签: animation calayer cakeyframeanimation

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

1 个答案:

答案 0 :(得分: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.);