【问题标题】:how to spin an image continuously如何连续旋转图像
【发布时间】:2012-11-08 14:46:16
【问题描述】:

我有一个 .png 格式的轮子图像,我想知道如何制作动画以使其连续旋转,我在 stackoverflow 上搜索并找到了某些帮助我旋转图像的 sn-ps 代码,但它不会连续旋转,它会旋转几秒钟然后停止,代码如下

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)

【问题讨论】:

标签: objective-c ios uiimageview caanimation


【解决方案1】:

您最好使用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"];

【讨论】:

  • 如果你需要把它放在viewDidLoad中,把它包在一个dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(NSEC_PER_MSEC * 1000)), dispatch_get_main_queue(), ^{ ... }块中
【解决方案2】:

下面的代码将连续旋转一个名为 iconViewUIImageView,直到 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 秒旋转)

像魔术一样工作!

【讨论】:

  • 什么是_rotate?
  • 这是一个全局布尔值。如果为 TRUE 则继续旋转,如果为 FALSE 则停止在其原始位置。我应该使用 if (self.keepRotating) {}
【解决方案3】:

这是使用计时器的另一种方法。 在您的 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);

}

希望这会有所帮助!

【讨论】:

    【解决方案4】:

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

    [UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionRepeat animations:^{
    
    image.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
    
        } completion:nil];
    

    【讨论】:

    • 我尝试了上述方法并使用了你的方法,但我遇到了同样的问题,即它旋转然后从同一个地方重新开始
    【解决方案5】:

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

     [UIView setAnimationRepeatCount:10000000];
    

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

    【讨论】:

    • 好吧——你只是旋转了 180 度,你期待什么?如果您希望它在重复之前旋转所有 360,只需将 180 替换为 360。
    • @deanWombourne..是的,这很直观,我尝试使用 360,但如果我这样做,它根本不会旋转..
    • 哦,这很烦人。我怀疑当您的变换被赋予 360 度旋转时,它会回绕回 0。您可能需要使用更复杂的 api 来实现这一点 - 您是否尝试过 @rckoenes 答案?
    • 好吧..我不知道为什么,但我一直收到有关quartzcoreframework的链接器错误..我没有添加quartzcoreframework,但即使添加后我也面临一些链接器错误问题
    • 我收到两个错误和一个警告。它们如下 ld:警告:忽略文件 /Users/apple/Desktop/justforcells/QuartzCore.framework/QuartzCore,文件是为不受支持的文件格式构建的不是被链接的架构 (i386) 架构 i386 的未定义符号:“_OBJC_CLASS_$_CALayer”,引用自:ViewController.o 中的 objc-class-ref “_OBJC_CLASS_$_CABasicAnimation”,引用自:ViewController 中的 objc-class-ref。 o ld:未找到架构 i386 的符号
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多