实现loading动画效果

下面我就来罗列三种实现loading动画效果的方法。

方法一:使用UIImageView自带的方法来实现,这也是我推荐的实现方法。

 

  1. NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"1.png"],[UIImage imageNamed:@"2.png"],[UIImage imageNamed:@"3.png"],[UIImage imageNamed:@"4.png"],[UIImage imageNamed:@"5.png"], nil nil];  
  2.   
  3. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 80, 30)];  
  4.   
  5. imageView.animationImages = array; //动画图片数组  
  6. imageView.animationDuration = 2; //执行一次完整动画所需的时长  
  7. //    gifImageView.animationRepeatCount = 0;  //动画重复次数 0表示无限次,默认为0  
  8. [imageView startAnimating];  
  9. [self.view addSubview:imageView];  

 

方法二:使用UIImageView+NSTimer(定时器)来实现,如果没有猜错的话,第一种方式内部的实现方法也是使用了定时器,只不过我们第二种方法是自己DIY一个属于自己的

控件,自己想要扩展什么功能就扩展什么。

 

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface imageAnimation : UIImageView  
  4.   
  5. @property (strong, nonatomic) NSTimer *animation_timer;  
  6.   
  7. @property (strong, nonatomic) NSMutableArray *imageArray;  
  8.   
  9. @property (assign) int currentIndex;  
  10.   
  11. @property (assign) float interval;  
  12. - (void)startLoading;  
  13.   
  14. - (void)stopLoading;  
  15.   
  16. - (void)initLoadingView:(NSMutableArray *)imageArray timeInterval:(float)time;  
  17.   
  18. @end  

 

 

  1. #import "imageAnimation.h"  
  2.   
  3. @implementation imageAnimation  
  4. @synthesize animation_timer = _animation_timer;  
  5. @synthesize imageArray = _imageArray;  
  6. @synthesize currentIndex = _currentIndex;  
  7. @synthesize interval = _interval;  
  8.   
  9.   
  10. - (id)initWithFrame:(CGRect)frame  
  11. {  
  12.     self = [super initWithFrame:frame];  
  13.     if (self) {  
  14.         // Initialization code  
  15.           
  16.     }  
  17.     return self;  
  18. }  
  19.   
  20. /* 
  21. // Only override drawRect: if you perform custom drawing. 
  22. // An empty implementation adversely affects performance during animation. 
  23. - (void)drawRect:(CGRect)rect 
  24.     // Drawing code 
  25. */  
  26.   
  27. - (void)initLoadingView:(NSMutableArray *)imageArray timeInterval:(float)time{  
  28.     self.imageArray = imageArray;  
  29.     self.interval = time;  
  30.       
  31.     self.animation_timer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(startLoading) userInfo:nil repeats:YES];  
  32. }  
  33.   
  34. //开始loading  
  35. - (void)startLoading{  
  36.     if(!self.imageArray || self.imageArray.count < 1){  
  37.         [self.animation_timer invalidate];  
  38.         return;  
  39.     }  
  40.       
  41.     self.currentIndex = (self.currentIndex +1)%self.imageArray.count;  
  42.     self.image = [UIImage imageNamed:[self.imageArray objectAtIndex:self.currentIndex]];  
  43. }  
  44.   
  45. //结束loading  
  46. -(void)stopLoading{  
  47.     [self.animation_timer invalidate];  
  48. }  
  49.   
  50.   
  51. @end  


方法三:使用UIWebView来加载gif图片,除非你要用到webView,不然就不要使用这种方式来实现

 

    1. NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"1" ofType:@"gif"]];  
    2. // view生成  
    3. UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(100, 100, 70, 30)];  
    4. webView.userInteractionEnabled = NO;//用户不可交互  
    5. [webView loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];  
    6. [self.view addSubview:webView]; 
posted @ 2016-04-14 09:35  橘子与布丁  阅读(435)  评论(0编辑  收藏  举报