发现这个问题的背景是,当时在做一个识别扫描二维码的功能。在扫码界面按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后动画暂停问题的解决办法
网友评论