最近有朋友问我类似微信语音播放的喇叭动画和界面图片加载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. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 80, 30)];
  2. imageView.animationImages = array; //动画图片数组
  3. imageView.animationDuration = 2; //执行一次完整动画所需的时长
  4. //    gifImageView.animationRepeatCount = 0;  //动画重复次数 0表示无限次,默认为0
  5. [imageView startAnimating];
  6. [self.view addSubview:imageView];

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

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

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

#import "imageAnimation.h"

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

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

ios开发之简单实现loading动画效果的更多相关文章

  1. [Swift通天遁地]五、高级扩展-(11)图像加载Loading动画效果的自定义和缓存

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. 实现loading动画效果

    下面我就来罗列三种实现loading动画效果的方法. 方法一:使用UIImageView自带的方法来实现,这也是我推荐的实现方法. NSMutableArray *array = [[NSMutabl ...

  3. iOS CAShapeLayer、CADisplayLink 实现波浪动画效果

    iOS CAShapeLayer.CADisplayLink 实现波浪动画效果 效果图 代码已上传 GitHub:https://github.com/Silence-GitHub/CoreAnima ...

  4. Atitit Loading 动画效果

    Atitit Loading 动画效果 使用才场景,加载数据,以及显示警告灯.. 要有手动关闭按钮 <div class="spinner loading_part" sty ...

  5. ios开发之--简单动画效果的添加

    记录一个简单的动画效果,自己写的,很简单,仅做记录. 附一个demo的下载地址: https://github.com/hgl753951/hglTest.git 代码如下: 1,准备 BOOL _i ...

  6. iOS开发笔记10:圆点缩放动画、强制更新、远程推送加语音提醒及UIView截屏

    1.使用CAReplicatorLayer制作等待动画 CALayer+CABasicAnimation可以制作很多简单的动画效果,之前的博客中介绍的“两个动画”,一个是利用一张渐变色图片+CABas ...

  7. iOS开发之虾米音乐频道选择切换效果分析与实现

    今天博客的内容比较简单,就是看一下虾米音乐首页中频道选择的一个动画效果的实现.之前用mask写过另外一种Tab切换的一种效果,网易云音乐里边的一种Tab切换效果,详情请移步于"视错觉:从一个 ...

  8. iOS开发-UIActivityIndicatorView简单使用

    软件开发的时候经常会遇到半天才加载出来数据的情况,不管是程序写的烂,还是说本来网速比较慢,一般都都会给个提示让用户感觉到我们在努力的加载数据,iOS可以通过UIActivityIndicatorVie ...

  9. loading动画效果记录

    看到好多网页都有一个炫酷的loading动画,以前不知道怎么实现的.今天学习了一下,发现其实也很简单. 首先在学习的时候偶然遇到一个pace.js的库,非常好用.优点是,不需要挂接到任何代码,自动检测 ...

随机推荐

  1. 使用VS2010命令提示窗口操作程序集强命名

    说明:文中示例均以将文件置于D盘根目录(D:\)下为例. 一.查看程序集是否具有强命名 sn -T d:\LicBase.dll 若有则会显示PublicKeyToken值,反之不会. 二.给无强命名 ...

  2. key-list类型内存数据引擎介绍及使用场景

    “互联网数据目前基本使用两种方式来存储,关系数据库或者key value.但是这些互联网业务本身并不属于这两种数据类型,比如用户在社会化平台中的关系,它是一个list,如果要用关系数据库存储就需要转换 ...

  3. WordPress 4.3.1正式发布 修复了3个安全问题

    WordPress 4.3.1正式发布  修复了3个安全问题! 出于安全性考虑,建议大家进行升级! WordPress 4.3.1 安全维护版本已经发布,该版本是针对之前所有版本的安全更新,强烈建议大 ...

  4. 关于socket通讯,如何才能高效?

    关于socket通讯,如何才能高效? 网络通讯,一个不朽的话题,今天和一个做游戏的朋友(以前的同事聊天),他向我诉说了他的痛苦 他之前是做客户端的,无奈人力资源紧张,也开始搞服务器,他说自己的服务器总 ...

  5. [置顶] Oracle学习路线与方法

    还没有整理好.... 1.学习路线 Oracle官方文档:2 Day DBA-->2 Day+Performance Tuning Guide--->Administrator's Gui ...

  6. 在路由器上搭建SVN服务器

    在路由器上搭建SVN服务器 SVN托管服务大家都不陌生了,我最早开始用的是谷歌提供的SVN,因为在上面托管的项目都是开源的,所以当有些项目不方便在网上公开的时候,就需要自己搭建SVN服务器了.wind ...

  7. jQuery Mobile (整合版)

    jQuery Mobile (整合版) 前言 为了方便大家看的方便,我这里将这几天的东西整合一下发出. 里面的例子请使用手机浏览器查看. 什么是jQuery Mobile? jquery mobile ...

  8. PHP之语言基础01 By ACReaper

    1.PHP中的变量是不需要声明类型的,由$标识变量,变量的命名规则也是字母或者下划线开头,接着任意字符或者下划线. $PI = 3.14; $radius = 5; $cir = $PI * 2 * ...

  9. 分享一个javascript alert精简框架

    如果你不喜欢浏览器自带的alert你可以尝试总共不超过10KB somke js  下载地址:http://smoke-js.com/ 使用方法 somke.alert("hello wor ...

  10. Android开发之Ubuntu上Eclipse不显示手机设备

    一.准备工作   A.开启Android设备,用USB数据线连接到Ubuntu系统.   B.启用设备的USB调试模试    C.启动Eclipse,在Devices栏会现一个有很多???号的不明设备 ...