10

这应该很简单,但我无法旋转UIImageView一个完整的 360 度,永远重复。

[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear | UIViewAnimationOptionBeginFromCurrentState animations:^{
    self.reloadButton.imageView.transform = CGAffineTransformRotate(self.reloadButton.imageView.transform, -M_PI);
} completion:^(BOOL finished) {

}];

根据文档,我传递的角度的符号CGAffineTransformRotate决定了旋转的方向,但上面的代码逆时针旋转。与 相同M_PI

该矩阵旋转坐标系轴的角度(以弧度为单位)。在 iOS 中,正值指定逆时针旋转,负值指定顺时针旋转。在 Mac OS X 中,正值指定顺时针旋转,负值指定逆时针旋转。

4

4 回答 4

14

Christoph 已经走上了正确的道路,但是有一种更好的方法可以让它保持旋转,而无需在每次结束时在动画代理中重新调用它。这是完全错误的。

只需将动画的 repeatCount 属性设置为HUGE_VALF.

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = @0.0f;
animation.toValue = @(2*M_PI);
animation.duration = 0.5f;             // this might be too fast
animation.repeatCount = HUGE_VALF;     // HUGE_VALF is defined in math.h so import it
[self.reloadButton.imageView.layer addAnimation:animation forKey:@"rotation"];

As stated in the documentation, this will cause the animation to repeat forever.

于 2012-05-08T18:12:06.533 回答
1

抱歉错过了第一部分,视图使用较小的角度旋转得很好:

[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear | UIViewAnimationOptionBeginFromCurrentState animations:^{
        redView.transform = CGAffineTransformRotate(redView.transform, M_PI_2);
    } completion:^(BOOL finished) {

    }];

我找不到任何有意义的解释为什么它会因 M_PI 而失败。

于 2012-05-08T07:54:58.800 回答
1

不久前我遇到了同样的问题。我不记得这个问题的原因,但这是我的解决方案:

/**
 * Incrementally rotated the arrow view by a given angle.
 *
 * @param degrees Angle in degrees.
 * @param duration Duration of the rotation animation.
 */
- (void)rotateArrowViewByAngle:(CGFloat)degrees
                  withDuration:(NSTimeInterval)duration {

    CABasicAnimation *spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    spinAnimation.fromValue = [NSNumber numberWithFloat:self.currentAngle / 180.0 * M_PI];
    spinAnimation.toValue = [NSNumber numberWithFloat:degrees / 180.0 * M_PI];
    spinAnimation.duration = duration;
    spinAnimation.cumulative = YES;
    spinAnimation.additive = YES;
    spinAnimation.removedOnCompletion = NO;
    spinAnimation.delegate = self;
    spinAnimation.fillMode = kCAFillModeForwards;

    [self.arrowView.layer addAnimation:spinAnimation forKey:@"spinAnimation"];

    self.currentAngle = degrees;
}

然后你可以使用委托方法

- (void)animationDidStart:(CAAnimation *)theAnimation
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag

旋转保持旋转。此外,程度和持续时间参数可能是非常高的数字......如果这足够了。

更新:
正如银口所说,

spinAnimation.repeatCount = HUGE_VALF;     // HUGE_VALF is defined in math.h so import it 

比在委托中重新启动动画要好得多。

请注意:
self.currentAngle 是一个记住当前最终旋转的属性。
我需要它来使视图左右旋转。

于 2012-05-08T14:50:13.513 回答
1

I wrote UIImageView category for this: https://gist.github.com/alexhajdu/5658543.

Just use:

[_myImageView rotate360WithDuration:1.0 repeatCount:0]; //0 for infinite loop 
于 2013-05-27T21:04:52.530 回答