iOS中MMDrawerController导航控制器的点击手势实现求助
Hey there! I’ve used MMDrawerController in several projects, so I can help you out with gesture implementations—since the official docs can be a bit sparse on this detail. Here’s a breakdown of how to work with its built-in gestures and add custom ones:
1. Enable/Disable Default Gestures
MMDrawerController comes with built-in pan and edge gestures for opening/closing the drawer. You can control which gestures are active using these properties:
// For opening the drawer self.drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll; // Enables all open gestures // Or pick specific modes: // MMOpenDrawerGestureModePanningNavigationBar // MMOpenDrawerGestureModePanningCenterView // MMOpenDrawerGestureModeBezelPanningCenterView // For closing the drawer self.drawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll; // Enables all close gestures // Specific modes: // MMCloseDrawerGestureModePanningCenterView // MMCloseDrawerGestureModePanningDrawer // MMCloseDrawerGestureModeTapCenterView
If you want to disable all gestures entirely, just set the mask to MMOpenDrawerGestureModeNone or MMCloseDrawerGestureModeNone.
2. Add Custom Gestures
If the default gestures don’t fit your use case, you can easily add custom gesture recognizers. For example, adding a double-tap gesture to toggle the drawer:
// Create a double-tap gesture recognizer UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleDrawerWithDoubleTap:)]; doubleTapGesture.numberOfTapsRequired = 2; // Add it to your center view (or navigation bar, drawer view, etc.) [self.drawerController.centerViewController.view addGestureRecognizer:doubleTapGesture];
Then implement the handler method:
- (void)toggleDrawerWithDoubleTap:(UITapGestureRecognizer *)gesture { if (gesture.state == UIGestureRecognizerStateEnded) { // Toggle the left drawer (use MMDrawerSideRight for right drawer) [self.drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil]; } }
You can replicate this pattern with UIPanGestureRecognizer too, if you want to define a custom pan area or behavior.
3. Resolve Gesture Conflicts
If your center view has scrollable content (like a UITableView or UIScrollView), you might run into gesture conflicts with MMDrawerController’s pan gestures. To fix this, implement the UIGestureRecognizerDelegate method:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { // Check if it's the drawer's pan gesture if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && gestureRecognizer == self.drawerController.panGestureRecognizer) { CGPoint translation = [(UIPanGestureRecognizer *)gestureRecognizer translationInView:self.view]; // Allow the drawer gesture only if the pan is horizontal (for left/right drawers) if (fabs(translation.x) > fabs(translation.y)) { return YES; } // Let the scroll view handle vertical pans return NO; } return YES; }
Don’t forget to set the delegate for the drawer’s gesture recognizer:
self.drawerController.panGestureRecognizer.delegate = self;
Hope this helps you get the gesture behavior you need! Let me know if you need further tweaks.
内容的提问来源于stack exchange,提问作者phadte viraj

