Specify direction of “show” (push) segue

白昼怎懂夜的黑 提交于 2019-11-30 15:11:23

问题


Is there a simple way to specify the direction (from left, right, top, or bottom) of a "show" segue (previously called a "push" segue)?

The default "show" segue goes from left to right, but I would like it to go from bottom to top.


回答1:


I don't believe there is a built in method for achieve that, however you could use Custom Transitions, which were introduced in iOS7. There are plenty of great tutorials out there, I've added some links below and some sample code demonstrating how you could slide the new view controller down from the top.

Sites to check out:

  • WWDC 2013 - Custom Transitions Using View Controllers

  • Animated Transitions in Swift / iOS8

  • Introduction to Custom View Controller Transitions and Animations

  • RayWenderlich: How To Make A View Controller Transition Animation Like in the Ping App

(Note: the way a UIViewController's view is accessed in the third and fourth tutorials is outdated. Instead of toViewController.view you should use transitionContext.viewForKey(UITransitionContextToViewKey))

An example:

Firstly, you're going to need to set up your storyboard, which I'm going to assume you've already done.

Secondly, you need an NSObject subclass that conforms to UIViewControllerAnimatedTransitioning. This will be used to control the transition from one view controller to the next.

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

As described in the RayWenderlich: How To Make A View Controller Transition Animation Like in the Ping App tutorial, you'll need to setup a your UINavigationController's delegate. The following NavigationControllerDelegate class will be used to return an instance of TransitionManager when a push segue is being conducted:

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

And that should be it! To expand on this I'd recommend you take a look at Interactive Transitions.



来源:https://stackoverflow.com/questions/30551909/specify-direction-of-show-push-segue

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