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

问题描述:

我正在使用自定义的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不显示该黑色动画,因为目标视图控制器(前一个源视图控制器)当然已经加载。

+0

不要忘记打电话'super.perform()' – erenkabakci

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

试试这个,而不是dst?.frame = CGRect(screenWidth, 0.0, screenWidth, screenHeight)

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

+0

这就是问题所在。忽略改变框架的坐标。谢谢,就是找不到它! –