iOS开发动画(Animation)图片360度不停旋转

iOS开发动画(Animation)图片360度不停旋转
最开始的想法是让旋转的弧度从0到2 * M_PI,  让动画不停的repeat,实验了一下,没有任何效果,系统动画的时候看到0与2 *M_PI是同一起一始点,所以没有效果。

后来想到一种办法,就是一个变量不断的累加变大,这样影响弧度也随着变化,就达到了圆周运动的效果。


直接上代码:

  1. -(void) startAnimation  
  2. {  
  3.     [UIView beginAnimations:nil context:nil];  
  4.     [UIView setAnimationDuration:0.01];  
  5.     [UIView setAnimationDelegate:self];  
  6.     [UIView setAnimationDidStopSelector:@selector(endAnimation)];  
  7.     imageView.transform = CGAffineTransformMakeRotation(angle * (M_PI / 180.0f));  
  8.     [UIView commitAnimations];  
  9. }  
  10.   
  11. -(void)endAnimation  
  12. {  
  13.     angle += 10;  
  14.     [self startAnimation];  
  15. }  

当然你可以用block的方式写也是一样的。

  1. - (void)startAnimation  
  2. {  
  3.     CGAffineTransform endAngle = CGAffineTransformMakeRotation(imageviewAngle * (M_PI / 180.0f));  
  4.       
  5.     [UIView animateWithDuration:0.01 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{  
  6.         imageView.transform = endAngle;  
  7.     } completion:^(BOOL finished) {  
  8.         angle += 10; [self startAnimation];  
  9.     }];  
  10.       
  11. }  


PS:其中angle是double类型。


这下就可以360度旋转了。


还有一种方法:

  1. CABasicAnimation* rotationAnimation;  
  2. rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];  
  3. rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];  
  4. rotationAnimation.duration = duration;  
  5. rotationAnimation.cumulative = YES;  
  6. rotationAnimation.repeatCount = repeat;  
  7.   
  8. [_loadingView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];  

reference:https://github.com/jonasschnelli/UIView-i7Rotate360


/


1 {
 2     CGFloat angle;
 3 }
 4 
 5 - (void)viewDidLoad {
 6     [super viewDidLoad];
 7     angle = 0;
 8     [self startAnimation];
 9 }
10 
11 //方法1
12 -(void) startAnimation
13 {
14     [UIView beginAnimations:nil context:nil];
15     [UIView setAnimationDuration:0.01];
16     [UIView setAnimationDelegate:self];
17     [UIView setAnimationDidStopSelector:@selector(endAnimation)];
18     self.scanImage.transform = CGAffineTransformMakeRotation(angle * (M_PI / 180.0f));
19     [UIView commitAnimations];
20 }
21 
22 -(void)endAnimation
23 {
24     angle += 2;
25     [self startAnimation];
26 }
27 
28 //方法2
29 - (void)startAnimation
30 {
31     CGAffineTransform endAngle = CGAffineTransformMakeRotation(angle * (M_PI / 180.0f));
32     
33     [UIView animateWithDuration:0.01 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
34         self.scanImage.transform = endAngle;
35     } completion:^(BOOL finished) {
36         angle += 2; [self startAnimation];
37     }];
38 }
复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值