Suppose I want to move a layer
from point A to B then to C:
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
animation.values = @[pointA, pointB, pointC];
animation.duration = 1;
After adding the animation to the layer, I set its speed to 0:
animation.speed = 0;
So I can use a slider to adjust the layer's position:
layer.timeOffset = slider.value;
But if my layer's current position is at pointB
, after adding the animation it moves back to pointA
even I set the layer
's timeOffset
to 0.5.
Is there anything I missed?
Thanks!
Update: To better illustrate the problem, here's some test code:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.testLayer = [CALayer layer];
self.testLayer.frame = CGRectMake(100, 100, 30, 30);
self.testLayer.backgroundColor = [UIColor redColor].CGColor;
[self.view.layer addSublayer:self.testLayer];
}
- (IBAction)action:(id)sender
{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.y"];
animation.values = @[@50, @100, @150];
animation.duration = 3.0f;
[self.testLayer addAnimation:animation forKey:@"animation"];
self.testLayer.speed = 0;
}
The testLayer
's Y position is 100, while the animation added has key values 50, 100 and 150, when the animation's added, testLayer
would be moved to the first key value position which is 50, but how to prevent this? I tried to set self.testLayer.timeOffset = 0.5
but it doesn't help.