【问题标题】:Swift segue custom transitions layout before commit提交前 Swift segue 自定义转换布局
【发布时间】:2019-06-02 17:41:41
【问题描述】:

我有两个视图控制器,A 和 B。我可以通过转场从 A 转到 B,并且我通过实现 UISegue 类在转场上实现了淡入动画。但是,在设备旋转和展开过程中会出现问题(从 B 到 A,而在 B 中发生了旋转)。

出现的问题是要呈现的视图(视图 A)的布局错误。在转换不起作用之前调用 setNeedsUpdateConstraints()layoutSubviews() 之类的函数或类似函数。屏幕截图描述了以横向打开 A 然后执行到 B 的 segue 的情况。在 B 中时,设备会旋转回纵向并展开到 A。

转场的 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)
        })
    }

}

关于如何或在何处实现解决此布局问题的功能的任何线索?谢谢!

【问题讨论】:

    标签: ios iphone swift storyboard transition


    【解决方案1】:

    我已经设法修复它。我可以在执行动画之前设置帧。我也停止使用 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)
            })
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-28
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多