A UIKit custom modal transition that simulates an elastic drag. Written in Swift.
- Xcode 7 or higher
- iOS 7.0 or higher
- ARC
- Swift 2.0
####CocoaPods
iOS 8 or later
use_frameworks!
pod "ElasticTransition"
####Manual
Download and add ElasticTransition folder into your project.
First of all, in your view controller, create an instance of ElasticTransition
var transition = ElasticTransition()
override func viewDidLoad() {
super.viewDidLoad()
// customization
transition.edge = .Left
transition.sticky = false
transition.panThreshold = 0.3
transition.transformType = .TranslateMid
// ...
}
Simply assign the transition to your navigation controller's delegate
navigationController?.delegate =transition
In prepareForSegue, assign the transition to be the transitioningDelegate of the destinationViewController. Also, dont forget to set the modalPresentationStyle to .Custom
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
segue.destinationViewController.transitioningDelegate = transition
segue.destinationViewController.modalPresentationStyle = .Custom
}
In your modal view controller implement the ElasticMenuTransitionDelegate and provide the contentLength
class MenuViewController: UIViewController, ElasticMenuTransitionDelegate {
var contentLength:CGFloat = 320
// ...
}
First, construct a pan gesture recognizer
let panGR = UIPanGestureRecognizer(target: self, action: "handlePan:")
view.addGestureRecognizer(panGR)
Then implement your gesture handler and fo the following:
func handlePan(pan:UIPanGestureRecognizer){
if pan.state == .Began{
// Here, you can do one of two things
// 1. show a viewcontroller directly
let nextViewController = // construct your VC ...
transition.startInteractiveTransition(self, toViewController: nextViewController, gestureRecognizer: pan)
// 2. perform a segue
transition.startInteractiveTransition(self, segueIdentifier: "menu", gestureRecognizer: pan)
}else{
transition.updateInteractiveTransition(gestureRecognizer: pan)
}
}
- Implement ElasticMenuTransitionDelegate in your modal view controller and set
var dismissByBackgroundTouch = true
var dismissByBackgroundDrag = true
var dismissByForegroundDrag = true
- Or use your own panGestureRecognizer and call dissmissInteractiveTransition in your handler
func handlePan(pan:UIPanGestureRecognizer){
if pan.state == .Began{
transition.dissmissInteractiveTransition(self, gestureRecognizer: pan, completion: nil)
}else{
transition.updateInteractiveTransition(gestureRecognizer: pan)
}
}
- Better Guide and Documentation
- Testing
Luke Zhao, me@lkzhao.com
ElasticTransition is available under the MIT license. See the LICENSE file for more info.