如果应用程序进入后台,UIView 动画将不起作用

UIView animation doesn't work if app goes to background

我有一个小问题游戏,如果用户回答时间太长,它会超时并自动触发到特定屏幕的自定义转场。

    let firstClassView = self.sourceViewController.view
    let secondClassView = self.destinationViewController.view
    let screenWidth = UIScreen.mainScreen().bounds.size.width
    let screenHeight = UIScreen.mainScreen().bounds.size.height
    firstClassView.clipsToBounds = true
    secondClassView.clipsToBounds = true
    let secondClassFrame = CGRect(x: screenWidth, y: 0, width: screenWidth, height: screenHeight)
    secondClassView.frame = secondClassFrame
    guard let window = UIApplication.sharedApplication().keyWindow else {
        return
    }
    window.insertSubview(secondClassView, aboveSubview: firstClassView)
    if let questionVC = self.destinationViewController as? QuestionResultViewController {
        questionVC.prepareResultView()
    }
    UIView.animateWithDuration(0.4, animations: { () -> Void in
        firstClassView.frame = CGRectOffset(firstClassView.frame, -screenWidth, 0)
        secondClassView.frame = CGRectOffset(secondClassView.frame, -screenWidth, 0)
    }) {_ -> Void in
        self.sourceViewController.presentViewController(self.destinationViewController, animated: false, completion: nil)
    }

此代码和 segue 工作正常,除非用户在 segue 之前将应用程序发送到后台(并再次打开它)。在这种情况下,到达了 UIView.animate 块,但它的内容似乎永远不会被调用,所以没有动画播放! (我在那里有一个打印命令,但没有被触发)

如果我的描述令人困惑:

案例 1:

案例二:

为什么会这样?我尝试使用 layoutIfNeeded 及其变体,但没有任何效果。永远不会调用动画块的内容。 从我使用断点的测试来看,应用程序在到达 UIView 行之前完全停止(并在大约 10 秒后崩溃)。

原来和UIView块没有关系。我有一个自定义 UILabel class,它的 layoutSubviews 方法在应用程序进入后台后进入无法检测到的无限循环。

抱歉造成混淆。