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 に答える