6

我有一个 .png 格式的轮子图像,我想知道如何制作动画以使其连续旋转,我在 stackoverflow 上进行了搜索,发现某些代码片段可以帮助我旋转图像,但它不会连续旋转,它会只需旋转几秒就停止,代码如下

viewdidload中的代码

UIImageView *imageToMove =
[[UIImageView alloc] initWithImage:[UIImageimageNamed:@"horo_circle.png"]];
[self.view addSubview:imageToMove];

[self rotateImage:imageToMove duration:5.0 
            curve:UIViewAnimationCurveEaseIn degrees:180];

和动画

- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration 
          curve:(int)curve degrees:(CGFloat)degrees
{
    // Setup the animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:curve];
    [UIView setAnimationBeginsFromCurrentState:YES];

    // The transform matrix
    CGAffineTransform transform = 
    CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
    image.transform = transform;

    // Commit the changes
    [UIView commitAnimations];
}

以及导入后的以下几行

#define M_PI   3.14159265358979323846264338327950288   /* pi */

#define DEGREES_TO_RADIANS(angle) (angle / 180.0 * M_PI)
4

5 回答 5

49

你最好这样做CABasicAnimation

if ([self.spinnerOverlay animationForKey:@"SpinAnimation"] == nil) {
    CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.fromValue = [NSNumber numberWithFloat:0.0f];
    animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
    animation.duration = 10.0f;
    animation.repeatCount = INFINITY;
    [self.spinnerOverlay.layer addAnimation:animation forKey:@"SpinAnimation"];
}

在此代码中,我检查动画是否已设置好,无需再次设置。在spinnerOverlay您的情况下,UIImageView您要旋转。

停止动画:

  [self.spinnerOverlay.layer removeAnimationForKey:@"SpinAnimation"];
于 2012-11-08T14:54:42.073 回答
3

下面的代码将持续旋转一个名为iconView的UIImageView直到rotate == NO

图像将始终返回其原始位置。

- (void)performRotationAnimated
{
    [UIView animateWithDuration:0.5
                          delay:0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{

                         self.iconView.transform = CGAffineTransformMakeRotation(M_PI);
                     }
                     completion:^(BOOL finished){

                         [UIView animateWithDuration:0.5
                                               delay:0
                                             options:UIViewAnimationOptionCurveLinear
                                          animations:^{

                                              self.iconView.transform = CGAffineTransformMakeRotation(0);
                                          }
                                          completion:^(BOOL finished){

                                              if (_rotate) {

                                                  [self performRotationAnimated];
                                              }
                                          }];
                     }];
}

这是同一主题的变体。

(总持续时间:2 * 0.5 = 1 秒旋转)

像魔术一样工作!

于 2015-05-27T23:37:26.410 回答
2

这是使用计时器的另一种方法。在您的 viewController.h 文件中,您需要声明您的计时器和图像:

@interface ViewController : UIViewController

{

    IBOutlet UIImageView *spinningImage;

    NSTimer *spinTimer;

}

接下来,在您的 ViewController.m 中,添加此代码,并可以通过降低时间间隔或增加变换间隔来加快旋转速度。

-(void) viewDidLoad {
    spinTimer = [NSTimer scheduledTimerWithTimeInterval:.01 target: self selector:@selector(spinVoid) userInfo:nil repeats:YES];
}

-(void) spinVoid {

    spinningImage.transform = CGAffineTransformRotate(spinningImage.transform, 0.001);

}

希望这可以帮助!

于 2016-05-03T22:51:19.863 回答
1

现在我们在 iOS6,请考虑切换到基于块的动画(自 iOS 4.0 起可用)而不是经典方法。尝试这个:

[UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionRepeat animations:^{

image.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));

    } completion:nil];
于 2012-11-08T14:53:20.327 回答
-1

使用您为动画选择的方法:

 [UIView setAnimationRepeatCount:10000000];

或者,您可以UIViewAnimationOptionRepeat在 blocks 方法中指定标志animateWithDuration:delay:options:animations:completion:

于 2012-11-08T14:54:34.020 回答