Navigation Pop View When Swipe Right Like Instagram iPhone App.How I Achieve This

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?

Sample Image

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:

  1. Calculate the percentage of drag on the view
  2. .Begin: Specify which segue to perform and assign UINavigationController delegate. delegate will be needed for InteractiveTransitioning
  3. .Changed: UpdateInteractiveTransition with percentage
  4. .Ended: Continue remaining transitioning if drag 50% or more or higher velocity else cancel
  5. .Cancelled, .Failed: cancel transitioning


References:

  1. UIPercentDrivenInteractiveTransition
  2. https://github.com/visnup/swipe-left
  3. https://github.com/robertmryan/ScreenEdgeGestureNavigationController
  4. https://github.com/groomsy/custom-navigation-animation-transition-demo


Related Topics



Leave a reply



Submit