UIView AnimateWithDuration从不到达完成块

Guk*_*ki5 2 uiview ios animatewithduration

我有这段代码,我用一个漂亮的小动画切换了根视图控制器,它已经工作了几个月了……但是后来随机停止了工作。

UIView snapshot = [self.window snapshotViewAfterScreenUpdates:YES];
[viewController.view addSubview:snapshot];
self.window.rootViewController = viewController;
NSLog(@"check point 1");
[UIView animateWithDuration:0.3 animations:^{
     NSLog(@"check point 2");
    snapshot.layer.opacity = 0;
     NSLog(@"check point 3");
    snapshot.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5);
     NSLog(@"check point 4");
} completion:^(BOOL finished) {
    NSLog(@"check point 5");
    [snapshot removeFromSuperview];
     NSLog(@"check point 6");
}];
Run Code Online (Sandbox Code Playgroud)

我放入了这些检查点,所有通过检查点4触发的事件..但是5和6从未触发。我觉得很奇怪,因为即使失败,完成块仍然应该触发。

在加载的新的根视图控制器上,请求用户收集其位置的权限。所以也许当弹出窗口出现时,它会破坏这种过渡吗?它曾经不习惯。

Pra*_*ysa 7

如果没有任何动画或源代码的其他部分已经完成了过渡部分,并且代码段在不同的线程(UI线程之外)上运行,则不会调用完成块。因此,仅出于说明目的,以下代码的完成块将永远不会触发,

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"check point 1");
        [UIView animateWithDuration:10.3 animations:^{
            NSLog(@"check point 4");
        } completion:^(BOOL finished) {
            NSLog(@"check point 5");
            [snapshot removeFromSuperview];
            NSLog(@"check point 6");
        }];
 });
Run Code Online (Sandbox Code Playgroud)

要解决此问题,请将您的代码包含在此代码中,

dispatch_async(dispatch_get_main_queue(), ^{
    //Your code, This runs on main thread
});
Run Code Online (Sandbox Code Playgroud)