【问题标题】:How do you rotate UIView, UIImageView or CALayer animated 360˚?你如何旋转 UIView、UIImageView 或 CALayer 动画 360˚?
【发布时间】:2011-03-26 19:07:45
【问题描述】:

如何在不使用 OpenGL ES 的情况下旋转 UIViewUIImageViewCALayer 动画 360 度?

【问题讨论】:

  • 您确实意识到 360° 旋转会为您提供完全相同的图像,对吧?
  • 我很伤心,在我意识到之前就开始尝试思考解决方案。
  • @Steam Trout:仅当您希望它是即时的。
  • 请接受答案?或者描述为什么它不能回答你的问题。

标签: iphone ios objective-c uikit core-animation


【解决方案1】:
#import <QuartzCore/QuartzCore.h>

CABasicAnimation *fullRotation;
fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = @(0.0);
fullRotation.toValue = @((360.0 * M_PI)/180.0);
fullRotation.duration = 3.5f;
fullRotation.repeatCount = MAXFLOAT;

[view.layer addAnimation:fullRotation forKey:@"rotation-animation"];   

如果你想改变它旋转的锚点,那么你可以这样做

CGRect frame = view.layer.frame;
self.anchorPoint = CGPointMake(0.5, 0.5); // rotates around center
self.frame = frame;

【讨论】:

    【解决方案2】:

    大概您的意思是动画将UIImage 旋转 360°?根据我的经验,完成一整圈旋转的方法是将多个动画链接在一起:

    - (IBAction)rotate:(id)sender
    {
       [UIView beginAnimations:@"step1" context:NULL]; {
          [UIView setAnimationDuration:1.0];
          [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
          [UIView setAnimationDelegate:self];
         [imageView.transform = CGAffineTransformMakeRotation(120 * M_PI / 180);
       } [UIView commitAnimations];
    }
    
    - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
    {
       if ([animationID isEqualToString:@"step1"]) {
          [UIView beginAnimations:@"step2" context:NULL]; {
             [UIView setAnimationDuration:1.0];
             [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
             [UIView setAnimationDelegate:self];
             imageView.transform = CGAffineTransformMakeRotation(240 * M_PI / 180);
          } [UIView commitAnimations];
       }
       else if ([animationID isEqualToString:@"step2"]) {
          [UIView beginAnimations:@"step3" context:NULL]; {
             [UIView setAnimationDuration:1.0];
             [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
             [UIView setAnimationDelegate:self];
             imageView.transform = CGAffineTransformMakeRotation(0);
          } [UIView commitAnimations];
       }
    }
    

    我已将 easeIn/easeOut 曲线(而不是线性)保留在原处,因此您可以查看各个动画。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      相关资源
      最近更新 更多