博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios开发之简单实现loading动画效果
阅读量:7070 次
发布时间:2019-06-28

本文共 2817 字,大约阅读时间需要 9 分钟。

最近有朋友问我类似语音播放的喇叭动画和界面图片加载loading界面是怎样实现的,是不是就是一个gif图片呢!我的回答当然是否定了,当然不排除也有人用gif图片啊!下面我就来罗列三种实现loading动画效果的方法。

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

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

  1.   
  2. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 80, 30)];  
  3.   
  4. imageView.animationImages = array; //动画图片数组  
  5. imageView.animationDuration = 2; //执行一次完整动画所需的时长  
  6. //    gifImageView.animationRepeatCount = 0;  //动画重复次数 0表示无限次,默认为0  
  7. [imageView startAnimating];  
  8. [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  

#import "imageAnimation.h"  

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

 

 

以上就是三种方法实现播放动画的过程,记住使用的时候要注意释放内存,不然开销就会很大!若大家有什么问题,或更好的方法,欢迎大家跟我交流。

转载地址:http://ryhll.baihongyu.com/

你可能感兴趣的文章
linux和CentOS是什么关系;CentOS和RHEL是什么关系
查看>>
samba
查看>>
利用Python网络爬虫抓取微信好友的签名及其可视化展示
查看>>
Linux-Nginx代理
查看>>
计算机的系统组成简介---运维笔记
查看>>
Liunx nginx 的使用方法及模块
查看>>
DBA——表级数据恢复之路(一) 请下载附件查看
查看>>
自定义弹出框
查看>>
如何扩展ESXi虚拟机磁盘容量
查看>>
sqlserver 登录方式修改,由默认的windows账户改为用sa等sql server账户登录
查看>>
Apache+tomcat 快速部署Java环境
查看>>
获取Android控件尺寸
查看>>
强大的命令行工具wmic
查看>>
Powershell通过变量、数组批量添加DHCP保留地址
查看>>
引导过程和服务控制
查看>>
拖拽即可创建HTML5网站的建站平台
查看>>
我的友情链接
查看>>
mod_fastcgi和mod_fcgid的区别
查看>>
Delphi字节位操作
查看>>
百兆、千兆网线的做法
查看>>