CABasicAnimation返回到原来的位置,下一个动画之前(CABasicAnimation

2019-10-17 06:03发布

我想要实现基于用户输入旋转一个UIButton的方法。 在第一次输入时,它应该旋转45度到左边。 在第二个输入,它应该转动从它第一次旋转后停止的位置另一个45度。

但按钮回到它非常原始的位置开始第二动画之前。 以下是我使用的方法。

- (void)spinLayer:(CALayer *)inLayer duration:(CFTimeInterval)inDuration
        direction:(int)direction
{
    CABasicAnimation* rotationAnimation;

    // Rotate about the z axis
    rotationAnimation = 
    [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

    // Rotate 360 degress, in direction specified
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 1/2 * direction];

    // Perform the rotation over this many seconds
    rotationAnimation.duration = inDuration;

    rotationAnimation.removedOnCompletion = NO;
    rotationAnimation.fillMode = kCAFillModeForwards;

    // Set the pacing of the animation
    rotationAnimation.timingFunction = 
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    // Add animation to the layer and make it so
    [inLayer addAnimation:rotationAnimation forKey:@"rotationAnimation"];


}
文章来源: CABasicAnimation returns to the original position before the next animation