CABasicAnimation resets to initial value after animation completes

后端 未结 15 2533
闹比i
闹比i 2020-12-04 05:44

I am rotating a CALayer and trying to stop it at its final position after animation is completed.

But after animation completes it resets to its initial position.

15条回答
  •  隐瞒了意图╮
    2020-12-04 06:06

    You can simply set the key of CABasicAnimation to position when you add it to the layer. By doing this, it will override implicit animation done on the position for the current pass in the run loop.

    CGFloat yOffset = 30;
    CGPoint endPosition = CGPointMake(someLayer.position.x,someLayer.position.y + yOffset);
    
    someLayer.position = endPosition; // Implicit animation for position
    
    CABasicAnimation * animation =[CABasicAnimation animationWithKeyPath:@"position.y"]; 
    
    animation.fromValue = @(someLayer.position.y);
    animation.toValue = @(someLayer.position.y + yOffset);
    
    [someLayer addAnimation:animation forKey:@"position"]; // The explicit animation 'animation' override implicit animation
    

    You can have more information on 2011 Apple WWDC Video Session 421 - Core Animation Essentials (middle of the video)

提交回复
热议问题