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

时间:2023-03-09 18:03:05
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];

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

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