【问题标题】:UIView animation doesn't work if app goes to background如果应用程序进入后台,UIView 动画将不起作用
【发布时间】:2016-06-30 17:36:08
【问题描述】:

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

    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 块,但它的内容似乎永远不会被调用,所以没有动画播放! (我有一个打印命令,它没有被触发)

如果我的描述令人困惑:

案例一:

  • 用户到达问题屏幕

  • 问题的计时器超时

  • Segue 触发器

  • 到达动画块并完美播放

案例 2:

  • 用户到达问题屏幕

  • 将应用程序发送到后台

  • 再次打开应用

  • 问题的计时器超时

  • Segue 触发器

  • 到达动画块,但没有任何反应 -> ???

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

【问题讨论】:

    标签: swift uiviewanimation


    【解决方案1】:

    事实证明它与 UIView 块无关。我有一个自定义的 UILabel 类,它的 layoutSubviews 方法在应用程序进入后台后进入了无法检测到的无限循环。

    很抱歉给您带来了困惑。

    【讨论】:

    • 您的代码不是正确的自定义演示动画,因此如果出现问题,您不会感到惊讶。你不能只是弄乱框架并像那样做一个presentViewController。您需要编写适当的自定义演示动画。你的所作所为总是错。
    • 在我的例子中,这个例子是一个 UIStoryboardSegue 子类,你有什么例子吗?或者我不应该使用它?
    • 你完全可以在 UIStoryboardSegue 子类中做到这一点。 github.com/mattneub/Programming-iOS-Book-Examples/blob/master/…
    • 不幸的是,它没有解决问题。我相信我已经搞砸了一些限制。但是感谢您的文件!
    猜你喜欢
    • 2015-08-16
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 2011-02-01
    相关资源
    最近更新 更多