ios - 指定 "show"(推)segue 的方向

标签 ios objective-c swift segue

是否有一种简单的方法来指定“显示”转场(以前称为“推”转场)的方向(从左、右、上或下)?

默认的“显示”转场从左到右,但我希望它从下到上。

最佳答案

我认为没有内置方法可以实现这一点,但是您可以使用 iOS7 中引入的自定义过渡。那里有很多很棒的教程,我在下面添加了一些链接和一些示例代码,演示了如何将新的 View Controller 从顶部向下滑动。

要检查的站点:

(注意:在第三和第四教程中访问UIViewControllerview的方式已经过时了。取而代之的是toViewController.view你应该使用 transitionContext.viewForKey(UITransitionContextToViewKey))

一个例子:

首先,您需要设置 Storyboard,我假设您已经完成了。

其次,您需要一个符合UIViewControllerAnimatedTransitioningNSObject 子类。这将用于控制从一个 View Controller 到下一个 View Controller 的转换。

class TransitionManager : NSObject, UIViewControllerAnimatedTransitioning {
    let animationDuration = 0.5

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
        return animationDuration
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView()
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        containerView.addSubview(toView)

        let finalFrame  = containerView.bounds
        let initalFrame = CGRect(x: 0, y: -finalFrame.height, width: finalFrame.width, height: finalFrame.height)

        toView.frame = initalFrame
        UIView.animateWithDuration(animationDuration,
            animations: { toView.frame = finalFrame },
            completion: { _ in transitionContext.completeTransition(true) })
    }
}

RayWenderlich: How To Make A View Controller Transition Animation Like in the Ping App 中所述教程中,您需要设置 UINavigationController 的委托(delegate)。以下 NavigationControllerDelegate 类将用于在执行推送转场时返回 TransitionManager 的实例:

class NavigationControllerDelegate: NSObject, UINavigationControllerDelegate {
    let transitionManager = TransitionManager()

    func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        if operation == .Push {
            return transitionManager
        }

        return nil
    }
}

应该就是这样!要对此进行扩展,我建议您查看交互式过渡。

关于ios - 指定 "show"(推)segue 的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30551909/

相关文章:

ios - 什么是Swift预处理器相当于iOS版本检查比较?

ios - 当前 View Controller 后 subview 重置为原始框架

swift - 有没有办法强制在方法中使用类而不是子类?

ios - Instruments,增加iPhone应用程序的分配

ios - AVAudioEngine:一次启动一组 AVAudioPlayerNode

objective-c - 在Interface Builder中制作的UISearchDisplayController的contentView在哪里设置

ios - 维基百科图像未从 UIWebView 中的网络存档加载

ios - Xcode 段 Controller 错误

Swift 4 静态获取模型属性名称的值

objective-c - 使用未声明的类型 "NSManagedObjectModel"