2017-07-17 51 views
0

我正在使用自定義的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

回答

0

這可能是您的問題dst?.frame = CGRect(0.0, screenHeight, screenWidth, screenHeight)

試試這個,而不是dst?.frame = CGRect(screenWidth, 0.0, screenWidth, screenHeight)

您的原始行將目標視圖設置爲離開屏幕底部而不是離開屏幕右側邊緣。當您通過屏幕寬度爲視圖設置動畫時,目標將從屏幕正下方滑動到下方和左側。

+0

這就是問題所在。忽略改變框架的座標。謝謝,就是找不到它! –

相關問題