【发布时间】:2017-07-17 14:03:55
【问题描述】:
我正在使用自定义 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 完美运行,但动画不是我想要的。当它“推动”目标视图控制器时,它显示为黑色。动画完成后,它才转向实际的视图控制器。我猜那是因为只有在动画完成后才会加载新的视图控制器。但是我将如何去预加载新的视图控制器,让动画看起来很流畅? unwinding segue 不会显示黑色动画,因为目标视图控制器(之前的源视图控制器)当然已经加载了。
【问题讨论】:
-
别忘了调用'super.perform()'