如何使用动画使UIView、UIImageView或CALayer旋转360度?

5

如何在不使用OpenGL ES的情况下,使UIView, UIImageViewCALayer沿着360度旋转的动画?


3
你知道360度旋转会得到完全相同的图像,对吗? - SteamTrout
我觉得很遗憾,在意识到之前,我已经开始尝试想出一个解决方案了。 - jtbandes
@Steam Trout:只有在您希望它是即时的情况下才需要这样做。 - JeremyP
请接受答案吗?或者描述一下为什么它不能回答你的问题。 - hfossli
2个回答

28
#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曲线(而不是线性的),这样你就可以看到单独的动画效果。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接