按Home键后,再回到应用时动画停止

发现这个问题的背景是,当时在做一个识别扫描二维码的功能。在扫码界面按Home键退出来再回到应用,扫码动画就消失了。

至于什么是扫码动画,大家打开微信的扫描二维码就知道了,实际上就是一个细条状的图片在扫码区域内从上到下移动,不断循环。

分析思路:
1、我在扫码的试图控制器中实现了viewDidAppear:方法,按完Home键回到应用不是应该会调用viewDidAppear:方法吗?

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self authorizationStatus];
}

- (void)authorizationStatus {
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (status == AVAuthorizationStatusAuthorized) {
        [self.readView startScan];
    } else {
       [self.readView stopScan];
       UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Error" message:@"请检查是否开启相机权限" preferredStyle:UIAlertControllerStyleAlert];
       UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
       [alertVC addAction: okAction];
        
       [self presentViewController:alertVC animated:true completion:nil];
    }
}

2、为了验证这种想法,我加入了2条输出NSLog语句

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self authorizationStatus];
    NSLog(@"appear");
}

- (void)authorizationStatus {
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (status == AVAuthorizationStatusAuthorized) {
        [self.readView startScan];
        NSLog(@"status");
    } else {
        [self.readView stopScan];
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Error" message:@"请检查是否开启相机权限" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alertVC addAction: okAction];
        
        [self presentViewController:alertVC animated:true completion:nil];
    }
}

实践证明,回到应用时,根本没有调用viewDidAppear:方法,不过是我自作多情。

3、既然系统不会自动调用,那就由我手动调用吧,在viewDidLoad:方法中添加通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewDidAppear:) name:UIApplicationWillEnterForegroundNotification object:nil];

</br>

相关资料链接:

1、为什么viewDidAppear不调用
2、CABasicAnimation 按home键后台之后,再切回来动画就停止了
3、iOS CAlayer 动画进入后台或者 调用viewWillDisappear viewDidDisappear后动画暂停问题的解决办法

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,219评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,810评论 4 61
  • 那天,去探望刚生下宝宝的珏。到了医院,上了楼,进入产科。 这绝对是每个医院最有人情味的地方,也是医院里能让人快乐、...
    粉妖阅读 2,994评论 0 1
  • 孩子的成长标准有很多,比如说人品是否高洁是一个标准,个性是否健康是一个标准,做事是否有创造力、想象力是一个标准,有...
    不断问自己阅读 1,841评论 0 0
  • 晌午,日头如锅盖。灼热的光,把村口路上的土,给烤得沸沸扬扬。一辆白色的轿车,开了过来,有几条土狗,从墙边跳了出来,...
    半朽阅读 2,934评论 11 20

友情链接更多精彩内容