twotoasters / TWTSideMenuViewController

Side Menus for iOS 7
MIT License
847 stars 148 forks source link

Why not add gestures to control the open and close? #7

Open xhzengAIB opened 10 years ago

xhzengAIB commented 10 years ago

Hi! @vokalinteractive I think will improve the user experience

auto234875 commented 10 years ago

You can do that by registering the navigationController interactiveGestureRecognizer to the open side menu selector.

onmyway133 commented 10 years ago

+1

abshoff commented 10 years ago

Please have a look at https://github.com/twotoasters/TWTSideMenuViewController/pull/30.

auto234875 commented 10 years ago

Be careful if you do this as it interferes with the interactivePopGestureRecognizer in the other viewcontroller. Meaning, if you try the back swipe gesture to pop the other viewcontrollers, it will open the side menu instead, screwing up everything.

A crude way to work around this is to delegate what the interactivePopGestureRecognizer should do in every view controller manually. Meaning, in the mainViewController, your interactivePopGestureRecognizer should open the menu. In the VC that are pushed onto the mainVC, the interactivePopGestureRecognizer carry out the action [self.navigationController popToRootViewControllerAnimated:YES];

You can do this by making the interactivePopGestureRecognizer post certain types of notifications and making every VC register to the NSNotificationCenter when it first comes onto the scene.

You also must remove the observer to these notifications right before you seque. Not doing this will cause a major bug when the user push and pop VC very fast, the app will turn completely blank. A really huge headache to implement something so simple.

abshoff commented 10 years ago

Thank you very much for pointing this out. I agree that there is a problem with this method if the other view controller is an instance of UINavigationController. However, in the sample project, the other view controller is an instance of UITableViewController, which, as far as I can see, does not use an interactive gesture recognizer. Thus, for the sample project, my simple changes should be fine. Please correct me if I am wrong.

Maybe one should add a navigation view controller to the sample project and give a best practice approach on how to implement these gestures. This should at least reduce headaches for anyone using this side menu controller.

auto234875 commented 10 years ago

It's really dependent on your project. For mine, the mainVC is a UITableVC. The next VC that could get push segue are UIViewVC and UITableViewVC. You really have to worry about this if you use UINavigationController because the gesture will conflict.

To implement this, here's a sample code. -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ if (gestureRecognizer==self.navigationController.interactivePopGestureRecognizer) {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"popBack" object:nil];
}
return YES;

} -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter]removeObserver:self name:@"popBack" object:nil]; } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(openMenu) name:@"popBack" object:nil]; } -(void)viewDidLoad{ self.navigationController.interactivePopGestureRecognizer.delegate=self; }

In your other VC that will be pushed onto the UINavigationController stack, -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(anotherMethodThatPopstoRootVC) name:@"popBack" object:nil]; }

Also, don't try to get around this by making your interactivePopGestureRecognizer.delegate = nil when you segue. It just won't work and your menu will still open whenever users pop the VC.

onmyway133 commented 10 years ago

I found RESideMenu that supports pan gesture