简体   繁体   English

提交前的快速segue自定义过渡布局

[英]Swift segue custom transitions layout before commit

I have two view controllers, A and B. I can go from A to B via a segue and I have implemented a fade in animation on the segue by implementing a UISegue class. 我有两个视图控制器A和B。我可以通过Segue从A转到B,并且通过实现UISegue类在Segue上实现了淡入淡出的动画效果。 However, problems occur during rotation of the device and the unwind (from B to A, while rotation has occurred whilst in B). 但是,在设备旋转和展开时会发生问题(从B到A,而在B时发生旋转)。

The problem that occurs is that the view to present (view A) has a wrong layout. 出现的问题是要呈现的视图(视图A)的布局错误。 Calling functions like setNeedsUpdateConstraints() or layoutSubviews() or similar functions before the transitions aren't working. 在转换无效之前,请调用诸如setNeedsUpdateConstraints()layoutSubviews()类的函数或类似函数。 The screenshot describes the situation where A is opened in landscape and then the segue to B is performed. 屏幕截图描述了A在风景中打开然后对B进行定位的情况。 When in B, the device is rotated back to portrait and unwound to A. 在B中时,设备将旋转回纵向并退绕到A。

The Swift code for the segues: segues的Swift代码:

class SegueFadeOut: UIStoryboardSegue {

    override func perform() {
        UIView.transition(from: source.view, to: destination.view, duration: 5.5, options: [.transitionCrossDissolve]) { (success) in
            self.destination.dismiss(animated: false, completion: nil)
        }
    }

}

class SegueFadeIn: UIStoryboardSegue {

    override func perform() {
        let toViewController = self.destination
        let fromViewController = self.source

        let containerView = fromViewController.view.superview

        toViewController.view.alpha = 0.0
        containerView?.addSubview(toViewController.view)

        UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseInOut, animations: {
            toViewController.view.alpha = 1.0
        }, completion: { success in
            fromViewController.present(toViewController, animated: false, completion: nil)
        })
    }

}

Any clues on how or where to implement a function that will fix this layout problem? 关于如何或在何处实现可解决此布局问题的功能的任何线索? Thanks! 谢谢!

演出期间的过渡

I have managed to fix it. 我已经设法解决了。 I can set the frame right before executing the animation. 我可以在执行动画之前设置帧。 I also stopped using the UIView.transition(...) function, as it gave some errors during the dismissal of the fromViewController. 我也停止使用UIView.transition(...)函数,因为在关闭fromViewController的过程中出现了一些错误。

class SegueFadeOut: UIStoryboardSegue {

    override func perform() {
        let toViewController = self.destination
        let fromViewController = self.source

        let containerView = fromViewController.view.superview

        toViewController.view.alpha = 0.0
        // ADDED LINE BELOW
        toViewController.view.frame = fromViewController.view.frame

        containerView?.addSubview(toViewController.view)

        UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseInOut, animations: {
            toViewController.view.alpha = 1.0
        }, completion: { success in
            fromViewController.dismiss(animated: false, completion: nil)
        })
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM