UIView AnimateWithDuration永远不会到达完成块

时间:2016-06-20 16:56:52

标签: ios uiview 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");
}];

我放入这些检查点,一切都通过检查点4触发..但5和6从不触发。对我来说很奇怪,因为即使它失败了,完成块仍应该触发。

在加载的新根视图控制器上,请求用户收集其位置的权限。那么也许当弹出窗口出现时,它会破坏这种转变?它虽然没有用过。

1 个答案:

答案 0 :(得分:5)

如果没有任何动画或转换部分已经由源代码的其他部分完成并且代码片段在不同的线程(除了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");
        }];
 });

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

dispatch_async(dispatch_get_main_queue(), ^{
    //Your code, This runs on main thread
});