Creating a sliding segue in Swift

2019-03-22 08:25发布

问题:

I am trying to create a segue/transition between two View Controllers that "slides" to the next View Controller. What I mean by "slide" is that it only moves as much as the translation of a pan gesture(similar to Snapchat). If you could help me with this or just point me in the right direction, that would be great.

回答1:

That effect is achieved using custom interactive transitions, which were introduced in iOS7. Here are a few tutorials to check out:

  • See Custom Transitions Using View Controllers. https://developer.apple.com/videos/wwdc/2013/
  • http://www.thinkandbuild.it/ios7-custom-transitions/
  • http://objectivetoast.com/2014/04/14/interactive-transitions/
  • http://www.teehanlax.com/blog/custom-uiviewcontroller-transitions/
  • This one gives many examples of the effects that can be achieved. http://www.appcoda.com/custom-view-controller-transitions-tutorial/

When I was implementing this I found that, for reusability, it was best to have one subclass of UIPercentDrivenInteractiveTransition (which we'll call TransitionManager) that implemented the protocols UIViewControllerTransitioningDelegate, UINavigationControllerDelegate and UIViewControllerAnimatedTransitioning.

- Then, if you need to present a UIViewController modally with your custom transition: in prepareForSegue set:

destinationViewController.modalPresentationStyle = .Custom
destinationViewController.transitioningDelegate  = TransitionManager()

- If you're using a UINavigationController it's even easier, all you need to do it set the UINavigationController's delegate to your TransitionManager.

Hopefully that should start to make some sense once you've gone through the tutorials.