UIPanGestureRecognizer to pop UIViewController
I think what you are looking for is already built-in with the interactivePopGestureRecognizer
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
if you want to make some custom or different animation then I think you need to check transitions. Here's a good article to make custom transitions:
https://medium.com/swift2go/simple-custom-uinavigationcontroller-transitions-fdb56a217dd8
How to enable back/left swipe gesture in UINavigationController after setting leftBarButtonItem?
First set delegate in viewDidLoad:
self.navigationController.interactivePopGestureRecognizer.delegate = self;
And then disable gesture when pushing:
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
[super pushViewController:viewController animated:animated];
self.interactivePopGestureRecognizer.enabled = NO;
}
And enable in viewDidDisappear:
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
Also, add UINavigationControllerDelegate
to your view controller.
Instagram/FB-like back navigation w/ animation
I guess you might have come across the other SO thread answering this question natively.
All you have to do is modify the default gesture recogniser on iOS frame.
export function onNavigatedFrom(args: EventData) {
console.log("Adding gesture...");
const frame = (<Page>args.object).frame;
if (frame.ios && !(<any>frame)._gestureRecognizer) {
const controller = frame.ios.controller;
const popGestureRecognizer = controller.interactivePopGestureRecognizer;
const targets = popGestureRecognizer.valueForKey("targets");
if (targets) {
let gestureRecognizer = UIPanGestureRecognizer.alloc().init();
gestureRecognizer.setValueForKey(targets, "targets");
frame.nativeView.addGestureRecognizer(gestureRecognizer);
(<any>frame)._gestureRecognizer = gestureRecognizer;
}
}
}
export function onNavigatedTo(args: EventData) {
console.log("Back to root page, removing gesture...");
const frame = (<Page>args.object).frame;
if (frame.ios && (<any>frame)._gestureRecognizer) {
frame.nativeView.removeGestureRecognizer((<any>frame)._gestureRecognizer);
(<any>frame)._gestureRecognizer = null;
}
}
Playground Sample
I want to pop my view from particular view but it works like LIFO
I'd recommend taking a look at the class documentation for UINavigationController. Instead of just popping a single view controller, you can:
- Pop to a specific view controller with
popToViewController:animated:
- Pop all the way to the root with
popToRootViewControllerAnimated:
- Set a brand new stack of view controllers with
setViewControllers:animated:
All of these will be automatically animated (if specified) and avoid the "LIFO" method of operation you describe.
How can I implement drag right to dismiss a View Controller that's in a navigation stack?
Made a demo project in Github
https://github.com/rishi420/SwipeRightToPopController
I've used UIViewControllerAnimatedTransitioning
protocol
From the doc:
// This is used for percent driven interactive transitions, as well as for container controllers ...
Added a UIPanGestureRecognizer
to the controller's view. This is the action of the gesture:
func handlePanGesture(panGesture: UIPanGestureRecognizer) {
let percent = max(panGesture.translationInView(view).x, 0) / view.frame.width
switch panGesture.state {
case .Began:
navigationController?.delegate = self
navigationController?.popViewControllerAnimated(true)
case .Changed:
percentDrivenInteractiveTransition.updateInteractiveTransition(percent)
case .Ended:
let velocity = panGesture.velocityInView(view).x
// Continue if drag more than 50% of screen width or velocity is higher than 1000
if percent > 0.5 || velocity > 1000 {
percentDrivenInteractiveTransition.finishInteractiveTransition()
} else {
percentDrivenInteractiveTransition.cancelInteractiveTransition()
}
case .Cancelled, .Failed:
percentDrivenInteractiveTransition.cancelInteractiveTransition()
default:
break
}
}
Steps:
- Calculate the percentage of drag on the view
.Begin:
Specify which segue to perform and assignUINavigationController
delegate. delegate will be needed forInteractiveTransitioning
.Changed:
UpdateInteractiveTransition with percentage.Ended:
Continue remaining transitioning if drag 50% or more or higher velocity else cancel.Cancelled, .Failed:
cancel transitioning
References:
- UIPercentDrivenInteractiveTransition
- https://github.com/visnup/swipe-left
- https://github.com/robertmryan/ScreenEdgeGestureNavigationController
- https://github.com/groomsy/custom-navigation-animation-transition-demo
Related Topics
How to Hide 'Back' Button on Navigation Bar on Iphone
How to Present View Controller from Right to Left in iOS Using Swift
How to Limit Uitableview Row Reordering to a Section
iPhone Uitableview. How Do Turn on the Single Letter Alphabetical List Like the Music App
Are Headphones Plugged In? iOS7
How to Check Version of a Cocoapods Framework
iOS Autolayout Multi-Line Uilabel
How to Make an iOS Asset Bundle
How to Handle Push Notifications If the Application Is Already Running
Changing Tab Bar Item Image and Text Color iOS
Expanding and Collapsing Uitableviewcells with Datepicker
Certificate Has Either Expired or Has Been Revoked
Formatting Phone Number in Swift
How to Invoke iPhone Maps for Directions with Current Location as Start Address
Is There Any Way of Asking an iOS View Which of Its Children Has First Responder Status
Change Width of a Uibarbuttonitem in a Uinavigationbar