在显示目标视图控制器之前,自定义Segue动画为黑色

时间:2017-07-17 14:03:55

标签: ios swift segue

我使用自定义segue在两个视图控制器之间进行切换。这是我用于其中一个转换的segue:

class SegueFromRight: UIStoryboardSegue{
    override func perform() {
        // Assign the source and destination views to local variables.
        let src = self.source.view as UIView!
        let dst = self.destination.view as UIView!

        // Get the screen width and height.
        let screenWidth = UIScreen.main.bounds.size.width
        let screenHeight = UIScreen.main.bounds.size.height

        // Specify the initial position of the destination view.
        dst?.frame = CGRect(0.0, screenHeight, screenWidth, screenHeight)

        // Access the app's key window and insert the destination view above the current (source) one.
        let window = UIApplication.shared.keyWindow
        window?.insertSubview(dst!, aboveSubview: src!)

        // Animate the transition.
        UIView.animate(withDuration: 0.4, animations: { () -> Void in
            src?.frame = (src?.frame.offsetBy(dx: -screenWidth, dy: 0.0))!
            dst?.frame = (dst?.frame.offsetBy(dx: -screenWidth, dy: 0.0))!

        }) { (Finished) -> Void in
            self.source.present(self.destination as UIViewController,
                                                            animated: false,
                                                            completion: nil)
        }

    }
}

segue完美无缺,但动画不是我喜欢的方式。当它推动"目标视图控制器,它显示为黑色。一旦动画结束,它只会转向实际的视图控制器。我想这是因为只有动画结束后才会加载新的视图控制器。但是我如何预加载新的视图控制器,以便动画看起来流畅? 展开segue不显示黑色动画,因为目标视图控制器(先前的源视图控制器)当然已经加载。

1 个答案:

答案 0 :(得分:0)

这可能是您的问题dst?.frame = CGRect(screenWidth, 0.0, screenWidth, screenHeight)

请改为spritecollide

您的原始行将目标视图设置为偏离屏幕底部而不是关闭到屏幕的右边缘。当您按屏幕宽度为视图设置动画时,目标会从屏幕正下方滑动到下方和左侧。