How to configure animated in custom segue in iOS using swift?

混江龙づ霸主 提交于 2019-12-12 03:59:00

问题


I have a created a custom segue like below

class DismissSegue: UIStoryboardSegue {
override func perform() {
    sourceController.dismissViewControllerAnimated(true, completion: nil)
}
}

In some scenarios I want to dismiss my currentVC without animation but in some cases with animation. How does it possible? I tried setting up @IBInspectable but no luck it doesn't show up property in storyboard. Any kind of help is appreciated. Thanks.


回答1:


A couple of thoughts:

  • If you are determined to use your DismissSegue, you can, in Swift 3:

    class DismissSegue: UIStoryboardSegue {
        override func perform() {
            let animated = (source as? OptionalAnimation)?.animateDismiss ?? true
            source.dismiss(animated: animated)
        }
    }
    

    Or Swift 2.3:

    class DismissSegue: UIStoryboardSegue {
        override func perform() {
            let animated = (source as? OptionalAnimation)?.animateDismiss ?? true
            sourceController.dismissViewControllerAnimated(animated, completion: nil)
        }
    }
    

    Where

    protocol OptionalAnimation {
        var animateDismiss: Bool { get }
    }
    

    And, your originating view controller would (a) conform to this protocol; and (b) set the animateDismiss property.

  • Frankly, if you're just calling dismissViewControllerAnimated/dismiss in your perform method, I'd discourage you from using a segue like this. You generally would not use a segue for dismissing at all and just call dismissViewControllerAnimated/dismiss from a @IBAction in the view controller from which you are dismissing. You can then set the animated flag as you see fit more directly.

    Or if you did want to use a segue, you'd use an unwind segue. You could, for example, have one unwind segue with animation and one without (which you set in IB) and just initiate whichever was appropriate.

  • And if you were doing custom animation, you'd generally use custom transitions (see WWDC 2013 Custom Transitions Using View Controllers). Then you have control over what animations you do or do not want to perform. Clearly, this is more complicated, but if you're doing custom animations, this is the way to tackle it.



来源:https://stackoverflow.com/questions/42544659/how-to-configure-animated-in-custom-segue-in-ios-using-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!