gresrun / GHSidebarNav

A clone of the new Facebook iOS UI paradigm
Apache License 2.0
607 stars 136 forks source link

Anyway to disable pan gesture sometimes? #6

Closed txie closed 11 years ago

txie commented 11 years ago

I found if I have a navigation controller which has a wizard like a few steps, user swipe to navigation bar and reveal the side menu can be confusing. If there anyway to disable that, and make user can reveal side menu only at root view controller only?

Thanks.

gresrun commented 11 years ago

You can remove the pan gesture recognizer from the navigation controller's navigation bar for the views that you wish to disable that feature. You'll have to hold a reference to it (or recreate it) if you want to add it back later.

txie commented 11 years ago

Thanks, here is what I do:

  1. take pretty much everything as the sample code
  2. For the root view controller in the navigation controller, I do:
- (void)viewWillAppear:(BOOL)animated {
    // enable pan gesture
    NSArray* gestureRecognizers = [self.navigationController.navigationBar gestureRecognizers];
    for (UIGestureRecognizer *gestureRecognizer in gestureRecognizers) {
        if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
            gestureRecognizer.enabled = YES;
            break;
        }
    }
}
  1. For other view controllers (wizard-like step by step views), disable the PanGesture in viewDidLoad
    NSArray* gestureRecognizers = [self.navigationController.navigationBar gestureRecognizers];
    for (UIGestureRecognizer *gestureRecognizer in gestureRecognizers) {
        if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
            gestureRecognizer.enabled = NO;
            break;
        }
    }

It works OK, but is there any better ways?

gresrun commented 11 years ago

That seems about as simple as it can get. The only other thing I can think of is you could make it a method:

- (void)enableSwipeToRevealGesture:(BOOL)enable {
    NSArray* gestureRecognizers = [self.navigationController.navigationBar gestureRecognizers];
    for (UIGestureRecognizer *gestureRecognizer in gestureRecognizers) {
        if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
            gestureRecognizer.enabled = enable;
            break;
        }
    }
}

so it doesn't clutter your logic flow.