How do you rotate UIView, UIImageView or CALayer animated 360˚?

纵饮孤独 提交于 2019-12-09 00:40:46

问题


How do you rotate UIView, UIImageView or CALayer animated 360 degress without using OpenGL ES?


回答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"];   

If you want to change the anchor point it to rotates around then you can do

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



回答2:


Presumably you mean animate rotating a UIImage around a full 360°? In my experience, the way to accomplish a full circle rotation is to chain multiple animations together:

- (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];
   }
}

I've left the easeIn/easeOut curve (instead of linear) in place, so you can see the individual animations.



来源:https://stackoverflow.com/questions/3431776/how-do-you-rotate-uiview-uiimageview-or-calayer-animated-360%cb%9a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!