问题
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 yourperform
method, I'd discourage you from using a segue like this. You generally would not use a segue for dismissing at all and just calldismissViewControllerAnimated
/dismiss
from a@IBAction
in the view controller from which you are dismissing. You can then set theanimated
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