gpt4 book ai didi

ios - CAKeyframeAnimation 从最后一点开始重复旋转

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:18:52 26 4
gpt4 key购买 nike

在我的 iPhone 应用程序中,我有一个 UIButton,当另一个 UIButton 被按下时,我将它旋转 180 度进入 View ,然后当再次单击时,该按钮会进一步旋转 180 度回到它开始的位置。

这在第一次完整的 360 度过程发生时一切正常,但如果我尝试再次从头开始,它会捕捉 180 度,然后尝试从该点旋转它。谁能指出我正确的方向?到目前为止,这是我的代码...

showAnimation= [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
showAnimation.duration = self.showAnimationDuration;
showAnimation.repeatCount = 1;
showAnimation.fillMode = kCAFillModeForwards;
showAnimation.removedOnCompletion = NO;
showAnimation.cumulative = YES;
showAnimation.delegate = self;

float currentAngle =[[[rotateMe.layer presentationLayer] valueForKeyPath:@"transform.rotation.z"] floatValue];

//Rotate 180 degrees from current rotation
showAnimation.values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:currentAngle],
[NSNumber numberWithFloat:currentAngle + (0.5 * M_PI)],
[NSNumber numberWithFloat:currentAngle + M_PI], nil];

[rotateMe.layer addAnimation:showAnimation forKey:@"show"];

动画完成后,我将 rotateMe.transform 旋转更新为图层的旋转,使其可用。

- (void)animationDidStop:(CAKeyframeAnimation *)anim finished:(BOOL)flag
{
float currentAngle =[[[self.layer presentationLayer] valueForKeyPath:@"transform.rotation.z"] floatValue];

NSLog(@"End: %f", currentAngle);
rotateMe.transform = CGAffineTransformMakeRotation(0);
}

我已经用

实现了完整的工作效果
    [UIView animateWithDuration:1.0f]
animations:^{
CGAffineTransform transform = CGAffineTransformRotate(rotateMe.transform, DEGREES_TO_RADIANS(179.999f));
rotateMe.transform = transform;
}
];

但我想让动画更复杂,因此需要 CAKeyframeAnimation。

最佳答案

如果您需要所有关键帧,您可以将动画配置为相加 并在 0 到 180 度之间设置动画。如果您不需要不同的关键帧,您可以简单地使用基本动画和 byValue 属性来完成。下次添加动画时,它会比 View 上的任何旋转多旋转 180 度。

如果您在委托(delegate)回调中设置实际值,则不需要填充模式,也不需要在完成时移除动画。

CABasicAnimation *showAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
showAnimation.byValue = M_PI;
showAnimation.duration = self.showAnimationDuration;
showAnimation.delegate = self;

[rotateMe.layer addAnimation:showAnimation forKey:@"show"];

或使用关键帧动画(如上所述:使其从 0 度到 180 度添加动画)。

CAKeyframeAnimation *showAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
showAnimation.additive = YES; // Make the values relative to the current value
showAnimation.values = @[0, /*all your intermediate values here... ,*/ M_PI];
showAnimation.duration = self.showAnimationDuration;
showAnimation.delegate = self;

[rotateMe.layer addAnimation:showAnimation forKey:@"show"];

关于ios - CAKeyframeAnimation 从最后一点开始重复旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13297675/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com