ios - 自定义 Segue 在显示目标 View Controller 之前将动画变为黑色

标签 ios swift segue

我正在使用自定义 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/

相关文章:

ios - 在 LAN 中广播 UDP 消息以检测 IP 摄像机

iphone - OAuth 认证 iPhone

ios - iOS 是否通过手机信号塔或 Wifi 热点(即没有 GPS)知道手机的位置?

swift - 如何在 Swift 中打开邮件面板?苹果系统

Swift:将数据传递到 previousViewController 而不在新窗口中打开 viewController

ios 如何从 ios 访问谷歌分析报告

swift - 从 RSS 或网页解析 XML

ios - 无法从 Facebook 获取照片

ios - viewDidLoad() 在 prepareForSegue() 完成之前被调用 : Swift

swift - 需要在 Modal Segues 之后释放内存,但我需要两个 Segues 将数据从 A 传递到 B 以及从 B 传递到 A