做个旋转的 ActivityIndicator

来源:互联网 发布:长春盘古网络投诉电话 编辑:程序博客网 时间:2024/05/29 10:20

做了个indicator,本来以为挺简单,试了好几个方法,才最终搞定,写个博客纪念一下。

原理很简单,就是在UIWIndow上加一个View,然后上面有一个一直旋转的图片。主要问题也是出现在旋转上。

第一种方法是UIView 的animation:

    [UIViewbeginAnimations:nilcontext:NULL];

    [UIViewsetAnimationRepeatCount:INT_MAX];

    [UIViewsetAnimationDuration:1.0f];

    view.transform =CGAffineTransformMakeRotation(M_PI);

    [UIViewcommitAnimations];

这个OK了,但只是转了180度,那就把角度换成2*M_PI吧;
换完不转了!!!!!!
哈哈,应该是旋转360度的话认为view没有改变,所以就没有动画了。。。。

然后就搜索怎么不停旋转。搜到的都是timer或者是不断改变角度值的方法的方法:
  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. }  

这段代码是从此处转载:http://blog.csdn.net/devday/article/details/7492469

用timer或者这种总是感觉不好啊!心理不踏实,最后还是去code4app下载了一个indicator的demo,发现还是用UIView的CALayer方法比较好!好在哪我也说不清楚。。。。

老前辈也给了我一份他写的,那就用这个吧!

-(CAAnimation *)animationRotate

{

   CATransform3D rotationTransform3D = CATransform3DMakeRotation(M_PI, 0.0, 0, 1.0);

   CABasicAnimation * animation;

    animation = [CABasicAnimationanimationWithKeyPath:@"transform"];

    animation.timingFunction=[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionLinear];

    animation.speed =1.5;

    animation.toValue = [NSValuevalueWithCATransform3D:rotationTransform3D];

    animation.duration =0.5f;

    animation.autoreverses =NO;

    animation.cumulative =YES;

    animation.repeatCount =FLT_MAX;

    animation.delegate =self;


   return animation;

}




总结:动画的东西还是用CALayer的方法比较好!好在哪里我也说不清楚。。。。。。


0 0
原创粉丝点击