ios开发之简单实现loading动画效果
2014-10-14 22:18
555 查看
最近有朋友问我类似微信语音播放的喇叭动画和界面图片加载loading界面是怎样实现的,是不是就是一个gif图片呢!我的回答当然是否定了,当然不排除也有人用gif图片啊!下面我就来罗列三种实现loading动画效果的方法。
方法一:使用UIImageView自带的方法来实现,这也是我推荐的实现方法。
方法二:使用UIImageView+NSTimer(定时器)来实现,如果没有猜错的话,第一种方式内部的实现方法也是使用了定时器,只不过我们第二种方法是自己DIY一个属于自己的
控件,自己想要扩展什么功能就扩展什么。
方法三:使用UIWebView来加载gif图片,除非你要用到webView,不然就不要使用这种方式来实现
以上就是三种方法实现播放动画的过程,记住使用的时候要注意释放内存,不然开销就会很大!若大家有什么问题,或更好的方法,欢迎大家跟我交流。QQ2058294890,请加注释,谢谢:)
方法一:使用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]; 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];
以上就是三种方法实现播放动画的过程,记住使用的时候要注意释放内存,不然开销就会很大!若大家有什么问题,或更好的方法,欢迎大家跟我交流。QQ2058294890,请加注释,谢谢:)
相关文章推荐
- ios开发之简单实现loading动画效果
- ios开发之简单实现loading动画效果
- IOS开发-CGAffineTransform的用法,实现一些简单动画效果
- iOS --用CABasicAnimation实现简单的动画效果
- iOS开发导航控制器下不同视图控制器之间切换:利用CATrasition和view的layer层来实现自定义的动画效果
- iOS开发-UIView之动画效果的实现方法(合集)
- ios开发之UIView动画效果实现二
- IOS开发之UIView动画效果的实现
- iOS开发雷达动画效果实现
- IOS开发-UIView之动画效果的实现方法
- 【iOS开发系列】简单动画效果
- 【iOS开发-113】在storyboard上用AutoLayout,纯代码实现AutoLayout布局方法以及简单动画
- iOS-实现简单的动画效果
- ios中开发在cell上实现内容的动画滑动效果
- IOS开发-UIView之动画效果的实现方法(合集)
- iOS开发仿淘宝添加到购物车的动画效果实现
- iOS-实现简单的动画效果
- iOS开发UIImage和UIImageView属性介绍,实现图片动画,实现开始/停止按钮效果
- 【iOS开发-24】导航控制器下不同视图控制器之间切换:利用CATrasition和view的layer层来实现自定义的动画效果
- 【iOS开发-50】利用创建新的类实现代码封装,从而不知不觉实践一个简单的MVC实验,附带个动画