CABasicAnimation resets to initial value after animation completes

后端 未结 15 2558
闹比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:14

    The problem with removedOnCompletion is the UI element does not allow user interaction.

    I technique is to set the FROM value in the animation and the TO value on the object. The animation will auto fill the TO value before it starts, and when it's removed will leave the object at it's correct state.

    // fade in
    CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath: @"opacity"];
    alphaAnimation.fillMode = kCAFillModeForwards;
    
    alphaAnimation.fromValue = NUM_FLOAT(0);
    self.view.layer.opacity = 1;
    
    [self.view.layer addAnimation: alphaAnimation forKey: @"fade"];
    

提交回复
热议问题