我的代码我的坑(七) —— UIImageView做序列帧动画结束后没有回调并且“隐藏”(一)

685 阅读3分钟
原文链接: www.jianshu.com

版本记录

版本号 时间
V1.0 2019.04.14 星期日

前言

做了好几个APP,碰到了大大小小的很多坑,以前碰到坑,解决了就结束了,这里想把自己碰到的坑记录下来,一来给自己备查二来希望可以帮助到大家。感兴趣的可以关注下,也欢迎大家补充留言,感兴趣的看上面几篇文章。
1. 我的代码我的坑(一) —— 自签名证书导致请求取消的问题(一)
2. 我的代码我的坑(二) —— UIImageView动画点击后动画和图片消失的问题(一)
3. 我的代码我的坑(三) —— iOS9系统WKWebView加载页面白板的问题(一)
4. 我的代码我的坑(四) —— iOS12系统自定义渐变色UISwitch手机横屏的异常问题(一)
5. 我的代码我的坑(五) —— 不可编辑状态的UITextView文本高度大于视图高度默认滚动到底部的问题(一)
6. 我的代码我的坑(六) —— UITextField输入长度自动截取时汉字和拼音带来的末位截取不能正常输入汉字的问题(一)

问题描述

大家在用UIImageView做序列帧动画的时候,总会碰到几个问题:

  • 1) 首先是startAnimating动画结束以后没有动画完成的API回调。
  • 2) 然后是动画完成以后如果不特殊逻辑设置,动画结束以后UIImageView就类似于隐藏了,看不到任何界面效果。
  • 3) 最后是注意内存和耗电的问题。

问题解决

首先看一下第一个问题,没有回调我们就只能计算,通过轮数和播放一轮的时间数的乘积获取到动画时间,然后执行一定的延时,在延时方法里做动画结束的逻辑。

[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self performSelector:@selector(animationDidFinish) withObject:self afterDelay:self.animationImageView.animationRepeatCount * self.animationImageView.animationDuration];

在执行之前别忘记先要cancel一下。

看一下第二个问题,看一下示例代码和效果实现。

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *animationImageView;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) NSMutableArray *imageArrM;

@end

@implementation ViewController

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self loadImageSource];
    
    [self.timer fire];
}

#pragma mark - Object Private Function

- (void)loadImageSource
{
    [self.imageArrM removeAllObjects];
    
    for(NSInteger i = 0; i < 25; i++){
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading%ld", i]];
        [self.imageArrM addObject:image];
    }
}

#pragma mark - Action && Notification

- (void)onTimerDidRun
{
    NSLog(@"onTimerDidRun");
    
    self.animationImageView.animationImages = [NSArray arrayWithArray:self.imageArrM];
    self.animationImageView.animationDuration = 1;
    self.animationImageView.animationRepeatCount = 3;
    [self.animationImageView startAnimating];
    
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [self performSelector:@selector(animationDidFinish) withObject:self afterDelay:3 * 1];
}

- (void)animationDidFinish
{
    [self.animationImageView stopAnimating];
}

#pragma mark - Getter && Setter

- (NSTimer *)timer
{
    if(!_timer){
        _timer = [NSTimer timerWithTimeInterval:10 target:self selector:@selector(onTimerDidRun) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    }
    return _timer;
}

- (NSMutableArray *)imageArrM
{
    if(!_imageArrM){
        _imageArrM = [NSMutableArray array];
    }
    return _imageArrM;
}

@end

下面看一下运行效果

这里看见每次循环三次动画然后UIImageView就"隐藏了",如果这个时候我们要有其他的需求,比如说动画结束后要展示一个静态图像怎么办?其实就需要在动画结束回调中给控件添加个image就可以了。

- (void)animationDidFinish
{
    self.animationImageView.image = [UIImage imageNamed:@"loading0"];
    [self.animationImageView stopAnimating];
}

下面看一下效果

这里就是在动画结束的回调中加入一个简单的给控件添加image的逻辑。

后记

本篇主要讲述了UIImageView做序列帧动画结束后没有回调并且“隐藏”的问题,感兴趣的给个赞或者关注~~~