iOS7 UINavigationController pushViewController :animated back to back with animation locks up the main thread

标签 ios objective-c animation uinavigationcontroller ios7

据我所知,似乎插入 UINavigationController背靠背动画在 iOS7 上造成了死锁。

我最初在 iOS6 上遇到崩溃并想出了以下解决方案:

  • 创建一个 View Controller 数组以在初始推送后推送到导航 Controller 。因此,如果要推送三个 View Controller (A、B 和 C),那么这个数组将有 B 和 C。
  • 实现UINavigationControllerDelegatenavigationController:didShowViewController:animated:
  • 在委托(delegate)方法中,只需检查 View Controller 数组中是否有更多元素。如果是这样,从其中取出第一个元素并将其推送到导航 Controller 中。
    所以本质上如果有 B 和 C View Controller 被推送,navigationController:didShowViewController:animated:将被调用 3 次;每次从 A 开始推送 View Controller 之后。显然,最后一次调用不会做任何事情,因为此时数组应该是空的。

  • 请注意,这种方法在 iOS6 上运行良好。但是,这在 iOS7 中中断了。似乎当它尝试在第二次推送中设置动画时,应用程序卡住了。在深入挖掘之后,我想出了在委托(delegate)实现中以以下方式推送第二个 View Controller 的解决方案。
    dispatch_async(dispatch_get_main_queue(), ^{
        [navigationController pushViewController:viewController 
                                        animated:YES];
    });
    

    这似乎解决了这个问题,但我想知道是否有人经历过类似的事情并对到底发生了什么有更好的解释。

    最佳答案

    我不确定你遇到了什么问题。我创建了一个具有导航 Controller 和其他四个 Controller 的测试应用程序。第一个 Controller 有一个按钮可以推送到第二个 Controller ,即 FirstPushedViewController。在那个 Controller 中,我有这段代码来推送接下来的两个 Controller ,它在 iOS 7 中运行良好:

    @interface FirstPushedViewController ()
    @property (strong,nonatomic) NSMutableArray *vcs;
    @end
    
    @implementation FirstPushedViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.navigationController.delegate = self;
        UIViewController *orange = [self.storyboard instantiateViewControllerWithIdentifier:@"Orange"];
        UIViewController *red = [self.storyboard instantiateViewControllerWithIdentifier:@"Red"];
        self.vcs = [@[red,orange] mutableCopy];
    }
    
    -(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
        if (self.vcs.count > 0) {
            [self.navigationController pushViewController:self.vcs.lastObject animated:YES];
            [self.vcs removeLastObject];
        }
    }
    

    关于iOS7 UINavigationController pushViewController :animated back to back with animation locks up the main thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19714288/

    相关文章:

    ios - 当用户点击 iOS 中本地通知上的“查看”按钮时,如何设置 View ?

    iphone - 当 NSLog 应用于它时,UILabel 没有在日志上提供文本。?

    css - 关键帧 CSS 动画问题 最新的 Chrome 浏览器

    html - 使用 CSS3 过渡

    Objective-C:类之间如何通信?

    ios - Xcode 5 框架/库搜索路径绝对地址

    ios - 如何在 UIImageview 中绘制椭圆形

    android - 有没有办法在 AnimatedVectorDrawables 中监听动画结束

    ios - 使用 cordova 工具集构建应用程序时出错

    objective-c - AVAudioPlayer 阻塞和 iOS 5 问题