如何使用swift在iOS中的自定义segue中配置动画? [英] How to configure animated in custom segue in iOS using swift?

查看:60
本文介绍了如何使用swift在iOS中的自定义segue中配置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个像下面这样的自定义转场

I have a created a custom segue like below

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

在某些情况下,我想在没有动画的情况下关闭我的 currentVC,但在某些情况下有动画.怎么可能?我尝试设置 @IBInspectable 但不幸的是它没有在故事板中显示属性.任何形式的帮助表示赞赏.谢谢.

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.

推荐答案

一些想法:

  • 如果您决定使用 DismissSegue,则可以在 Swift 3 中:

  • 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)
    }
}

或 Swift 2.3:

Or Swift 2.3:

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

哪里

protocol OptionalAnimation {
    var animateDismiss: Bool { get }
}

而且,您的原始视图控制器将 (a) 遵守此协议;(b) 设置 animateDismiss 属性.

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

坦率地说,如果您只是在 perform 方法中调用 dismissViewControllerAnimated/dismiss,我不鼓励您使用像这样的转场.您通常根本不会使用 segue 进行解散,只需从您所在的视图控制器中的 @IBAction 调用 dismissViewControllerAnimated/dismiss解雇.然后,您可以更直接地设置 animated 标志.

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.

或者,如果您确实想使用 segue,则可以使用 unwind segue.例如,您可以设置一个带动画的展开转场,另一个不带(您在 IB 中设置),然后选择合适的启动.

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.

如果您要制作自定义动画,通常会使用自定义过渡(请参阅 WWDC 2013 使用视图控制器的自定义转换).然后你可以控制你想要或不想执行的动画.显然,这更复杂,但如果您正在制作自定义动画,这就是解决它的方法.

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.

这篇关于如何使用swift在iOS中的自定义segue中配置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆