ios开发之简单实现loading动画效果
最近有朋友问我类似微信语音播放的喇叭动画和界面图片加载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];
- UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 80, 30)];
- imageView.animationImages = array; //动画图片数组
- imageView.animationDuration = 2; //执行一次完整动画所需的时长
- // gifImageView.animationRepeatCount = 0; //动画重复次数 0表示无限次,默认为0
- [imageView startAnimating];
- [self.view addSubview:imageView];
方法二:使用UIImageView+NSTimer(定时器)来实现,如果没有猜错的话,第一种方式内部的实现方法也是使用了定时器,只不过我们第二种方法是自己DIY一个属于自己的
控件,自己想要扩展什么功能就扩展什么。
- #import <UIKit/UIKit.h>
- @interface imageAnimation : UIImageView
- @property (strong, nonatomic) NSTimer *animation_timer;
- @property (strong, nonatomic) NSMutableArray *imageArray;
- @property (assign) int currentIndex;
- @property (assign) float interval;
- - (void)startLoading;
- - (void)stopLoading;
- - (void)initLoadingView:(NSMutableArray *)imageArray timeInterval:(float)time;
- @end
#import "imageAnimation.h"
- @implementation imageAnimation
- @synthesize animation_timer = _animation_timer;
- @synthesize imageArray = _imageArray;
- @synthesize currentIndex = _currentIndex;
- @synthesize interval = _interval;
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- }
- return self;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect
- {
- // Drawing code
- }
- */
- - (void)initLoadingView:(NSMutableArray *)imageArray timeInterval:(float)time{
- self.imageArray = imageArray;
- self.interval = time;
- self.animation_timer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(startLoading) userInfo:nil repeats:YES];
- }
- //开始loading
- - (void)startLoading{
- if(!self.imageArray || self.imageArray.count < 1){
- [self.animation_timer invalidate];
- return;
- }
- self.currentIndex = (self.currentIndex +1)%self.imageArray.count;
- self.image = [UIImage imageNamed:[self.imageArray objectAtIndex:self.currentIndex]];
- }
- //结束loading
- -(void)stopLoading{
- [self.animation_timer invalidate];
- }
- @end
方法三:使用UIWebView来加载gif图片,除非你要用到webView,不然就不要使用这种方式来实现
- NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"1" ofType:@"gif"]];
- // view生成
- UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(100, 100, 70, 30)];
- webView.userInteractionEnabled = NO;//用户不可交互
- [webView loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
- [self.view addSubview:webView];
以上就是三种方法实现播放动画的过程,记住使用的时候要注意释放内存,不然开销就会很大!若大家有什么问题,或更好的方法,欢迎大家跟我交流。
ios开发之简单实现loading动画效果的更多相关文章
- [Swift通天遁地]五、高级扩展-(11)图像加载Loading动画效果的自定义和缓存
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 实现loading动画效果
下面我就来罗列三种实现loading动画效果的方法. 方法一:使用UIImageView自带的方法来实现,这也是我推荐的实现方法. NSMutableArray *array = [[NSMutabl ...
- iOS CAShapeLayer、CADisplayLink 实现波浪动画效果
iOS CAShapeLayer.CADisplayLink 实现波浪动画效果 效果图 代码已上传 GitHub:https://github.com/Silence-GitHub/CoreAnima ...
- Atitit Loading 动画效果
Atitit Loading 动画效果 使用才场景,加载数据,以及显示警告灯.. 要有手动关闭按钮 <div class="spinner loading_part" sty ...
- ios开发之--简单动画效果的添加
记录一个简单的动画效果,自己写的,很简单,仅做记录. 附一个demo的下载地址: https://github.com/hgl753951/hglTest.git 代码如下: 1,准备 BOOL _i ...
- iOS开发笔记10:圆点缩放动画、强制更新、远程推送加语音提醒及UIView截屏
1.使用CAReplicatorLayer制作等待动画 CALayer+CABasicAnimation可以制作很多简单的动画效果,之前的博客中介绍的“两个动画”,一个是利用一张渐变色图片+CABas ...
- iOS开发之虾米音乐频道选择切换效果分析与实现
今天博客的内容比较简单,就是看一下虾米音乐首页中频道选择的一个动画效果的实现.之前用mask写过另外一种Tab切换的一种效果,网易云音乐里边的一种Tab切换效果,详情请移步于"视错觉:从一个 ...
- iOS开发-UIActivityIndicatorView简单使用
软件开发的时候经常会遇到半天才加载出来数据的情况,不管是程序写的烂,还是说本来网速比较慢,一般都都会给个提示让用户感觉到我们在努力的加载数据,iOS可以通过UIActivityIndicatorVie ...
- loading动画效果记录
看到好多网页都有一个炫酷的loading动画,以前不知道怎么实现的.今天学习了一下,发现其实也很简单. 首先在学习的时候偶然遇到一个pace.js的库,非常好用.优点是,不需要挂接到任何代码,自动检测 ...
随机推荐
- Python Learing(二):Basic Image Processing 1
<写在前面> Basic image processing 1: 0.(以简单的曲线图为例)对于生成的图自定义外观,使用子图,多个数据集,标题,标签,交互式标注,图例: 1.生成散点图,直 ...
- ssh配置公钥和私钥登陆SecureCRT
在用windows时管理linux服务器时,常会用到SecureCRT.Xshell以及开源的putty.在我工作环境大多都是采用密码认证的方式进行登录.今天对学习了些SecureCRT的密钥登录方式 ...
- CSS中文字体的英文名称 – 前台开发必备
做什么用的?写过CSS的都晓得,一般用在font-family后面——为什么不用中文呢?有过一定开发经验的都晓得CSS里面用中文也是会乱码的,特别是没有中文字符集的浏览器,直接成了框框,用英文就可以解 ...
- NuGet的本地服务器安装与Package的发布
NuGet的本地服务器安装与Package的发布 主要的步骤是按照下面的例子来做的: NuGet学习笔记(1)——初识NuGet及快速安装使用 NuGet学习笔记(2)——使用图形化界面打包自己的类库 ...
- 解决中文乱码( jsp表单提交中文时出现乱码)
有三种方法: 1.建立一个filter中文解决乱码 2.Struts2在struts.xml中修改默认的编码设定 3.用Spring解决中文乱码 4.直接在jsp中修改解决 1.建立一个filter解 ...
- Lazy<T>
Lazy<T> 对象的创建方式,始终代表了软件工业的生产力方向,代表了先进软件技术发展的方向,也代表了广大程序开发者的集体智慧.以new的方式创建,通过工厂方法,利用IoC容器,都以不同的 ...
- go语言中的数组切片:特立独行的可变数组
go语言中的数组切片:特立独行的可变数组 初看go语言中的slice,觉得是可变数组的一种很不错的实现,直接在语言语法的层面支持,操作方面比起java中的ArrayList方便了许多.但是在使用了一段 ...
- jQuery Mobile (中)
jQuery Mobile (中) 前言 昨天我们一起学习了一部分jquery mobile的知识,今天我们继续. 这些是些很基础的东西,有朋友觉得这个没有其它的好,但是学习下不吃亏嘛,我反正也不会一 ...
- linux下安装NPM管理工具
根据”挖一下“开发需要,选择nodejs实现异步IO,目的是为了解决服务器卡死导致无法处理后续的http请求.看了花瓣的架构视频讲座,才决定这么做的,挺有道理的. 安装nodejs很顺利,下载源码包, ...
- oracle保证读一致性原理
35 这里也有讲解 1:undo segment的概念 当数据库进行修改的时候,需要把保存到以前的old的数据保存到一个地方,然后进行修改,用于保存old数据的segment 就是undo segme ...