下面我就来罗列三种实现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. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 80, 30)];
  3. imageView.animationImages = array; //动画图片数组
  4. imageView.animationDuration = 2; //执行一次完整动画所需的时长
  5. //    gifImageView.animationRepeatCount = 0;  //动画重复次数 0表示无限次,默认为0
  6. [imageView startAnimating];
  7. [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
  1. #import "imageAnimation.h"
  2. @implementation imageAnimation
  3. @synthesize animation_timer = _animation_timer;
  4. @synthesize imageArray = _imageArray;
  5. @synthesize currentIndex = _currentIndex;
  6. @synthesize interval = _interval;
  7. - (id)initWithFrame:(CGRect)frame
  8. {
  9. self = [super initWithFrame:frame];
  10. if (self) {
  11. // Initialization code
  12. }
  13. return self;
  14. }
  15. /*
  16. // Only override drawRect: if you perform custom drawing.
  17. // An empty implementation adversely affects performance during animation.
  18. - (void)drawRect:(CGRect)rect
  19. {
  20. // Drawing code
  21. }
  22. */
  23. - (void)initLoadingView:(NSMutableArray *)imageArray timeInterval:(float)time{
  24. self.imageArray = imageArray;
  25. self.interval = time;
  26. self.animation_timer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(startLoading) userInfo:nil repeats:YES];
  27. }
  28. //开始loading
  29. - (void)startLoading{
  30. if(!self.imageArray || self.imageArray.count < 1){
  31. [self.animation_timer invalidate];
  32. return;
  33. }
  34. self.currentIndex = (self.currentIndex +1)%self.imageArray.count;
  35. self.image = [UIImage imageNamed:[self.imageArray objectAtIndex:self.currentIndex]];
  36. }
  37. //结束loading
  38. -(void)stopLoading{
  39. [self.animation_timer invalidate];
  40. }
  41. @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];

实现loading动画效果的更多相关文章

  1. ios开发之简单实现loading动画效果

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

  2. Atitit Loading 动画效果

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

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

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

  4. loading动画效果记录

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

  5. android动画介绍--Animation 实现loading动画效果

    Animation的使用方法并不难.这里简单的介绍一下使用方法. 先看效果图: 效果还是不错的吧. 下面来看看使用方法. 动画效果是通过Animation来实现的,一共有四种,分别为: AlphaAn ...

  6. CSS3效果:animate实现点点点loading动画效果(二)

    box-shadow实现的打点效果 简介 box-shadow理论上可以生成任意的图形效果,当然也就可以实现点点点的loading效果了. 实现原理 html代码,首先需要写如下html代码以及cla ...

  7. CSS3效果:animate实现点点点loading动画效果(一)

    实现如图所示的点点点loading效果: 一:CSS3 animation实现代码 html代码: 提交订单中<span class="ani_dot">...< ...

  8. css3实现的3中loading动画效果

    一.css3中animation动画各种属性详解: animation Value:     [<animation-name> || <animation-duration> ...

  9. javascript 通用loading动画效果

    由于项目中多处要给ajax提交的时候增加等待动画效果,所以就写了一个简单的通用js方法: 代码如下: /*ajax提交的延时等待效果*/ var AjaxLoding = new Object(); ...

随机推荐

  1. win dos命令行设置ip和dns

    首先以管理员身份运行cmd,保证之后在黑框中输入的命令都有管理员权限 C:\Windows\system32>netsh netsh>int netsh interface>ip n ...

  2. 每天一个linux命令---mount

    查询挂载服务的信息,使用挂载mount命令: [wapmail@app2linux04 monitor]$ mount |grep 172.16.182.146 type nfs (ro,udp,no ...

  3. iOS开发代码规范(通用)

    1. 关于命名 1> 统一要求 含义清楚,尽量做到不需要注释也能了解其作用,若做不到,就加注释 使用全称,不适用缩写 2> 类的命名 大驼峰式命名:每个单词的首字母都采用大写字母 例子:M ...

  4. HDU 3078 (LCA+树链第K大)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3078 题目大意:定点修改.查询树中任意一条树链上,第K大值. 解题思路: 先用离线Tarjan把每个 ...

  5. TC SRM 593 DIV1 250

    我只能说的亏没做,要不就挂0了.. 本来想四色定理,肯定4就可以的...然后准备爆,发现3的时候不好爆,又想了老一会,嗯,数据范围不小,应该不是暴力,直接找规律,貌似最大就是3,有一个3连块,输出3, ...

  6. 【CodeVS】 p1696 奇怪的函数

    题目描述 Description 自从得到上次的教训后,John的上课态度认真多了,也变得更爱动脑筋了.今天他又学习了一个新的知识:关于 xk 的位数. 如果x大于0小于l,那么位数=1+小数部分×k ...

  7. CF #374 (Div. 2) C. Journey dp

    1.CF #374 (Div. 2)    C.  Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...

  8. 20145330《Java学习笔记》第一章课后练习8知识总结以及IDEA初次尝试

    20145330<Java学习笔记>第一章课后练习8知识总结以及IDEA初次尝试 题目: 如果C:\workspace\Hello\src中有Main.java如下: package cc ...

  9. Redhat5.8 环境下编译安装 Redis 并将其注册为系统服务

    系统环境: $ cat /etc/issueRed Hat Enterprise Linux Server release 5.8 (Tikanga)Kernel \r on an \m 1. 下载安 ...

  10. Big Chocolate

    Big Chocolate 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19127 Big Chocolat ...