我正在使用自定义 Segue 来在两个 View Controller 之间进行转换。这是我用于其中一个转换的转场:
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 工作得很好,但是动画不是我想要的。当它“插入”目标 View Controller 时,它会显示为黑色。它只会在动画完成后立即转向实际的 View Controller 。我想这是因为新的 View Controller 只有在动画完成后才会加载。但是我该如何预加载新的 View Controller ,以使动画看起来更流畅呢? 展开转场不会显示黑色动画,因为目标 View Controller (之前的源 View Controller )当然已经加载。
最佳答案
这可能是您的问题dst?.frame = CGRect(0.0, screenHeight, screenWidth, screenHeight)
。
试试这个dst?.frame = CGRect(screenWidth, 0.0, screenWidth, screenHeight)
。
您的原始行将目标 View 设置为离开屏幕底部,而不是离开屏幕的右边缘。当您按屏幕宽度对 View 进行动画处理时,目标会从屏幕正下方滑动到下方和左侧。
关于ios - 自定义 Segue 在显示目标 View Controller 之前将动画变为黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45146382/