Ne AS
Ne AS

Reputation: 1550

MMDrawerController: detect menu close action or swipe gesture

I want to detect when the menu is closed. I found this SO solved question. In the comments of the accepted answer, they say that this method:

-(void)setGestureCompletionBlock:(void(^)(MMDrawerController * drawerController, UIGestureRecognizer * gesture))gestureCompletionBlock;

have to be in the App delegate in order to be notified when the menu is closed. I putted this function in my App delegate, and in it's implementation I made a NSLog(@"menu closed"); but nothing is printed in my console and the function is not fired when I close the menu.

Can anyone explain me please how can I detect that the menu is closed? (Based on the given answer or if you have another one)

Edit:

In the storyboard, I have an UINavigationController linked to an UIViewController having as type the MMDrawerController (myVCMMdrawerController), then I have also myCenterVC, the leftVC and the rightVC. In the myVCMMdrawerController viewDidLoad this is what I do:

MyCenterVC * centerVC = [[self storyboard] instantiateViewControllerWithIdentifier:@"myCenterVC"];
    centerVC.drawerController = self;

    LeftVC * leftVC = [[self storyboard] instantiateViewControllerWithIdentifier:@"leftVC"];
    leftVC.drawerController = self;

    self.centerViewController = centerVC;
    self.leftDrawerViewController = leftVC;


    self.showsShadow = false;
    //[self setMaximumLeftDrawerWidth:[UIScreen mainScreen].bounds.size.width animated: true completion: nil];

    //enable gesture
    self.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
    self.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll;
    [self closeDrawerAnimated:NO completion:nil];

In myCenterVC I make the actions to display the menu in IBActions like this:

[self.drawerController toggleDrawerSide:MMDrawerSideLeft animated:true completion:nil];

Upvotes: 0

Views: 756

Answers (1)

Lawrence Tan
Lawrence Tan

Reputation: 507

here's the code that works for me :

Swift 3.0+

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    let leftDrawer = UIViewController()
    leftDrawer.view.backgroundColor = UIColor.red
    let centerDrawer = UIViewController()
    centerDrawer.view.backgroundColor = UIColor.green

let mainContainer = MMDrawerController(center: centerDrawer, leftDrawerViewController: leftDrawer)

mainContainer?.openDrawerGestureModeMask = .panningCenterView
mainContainer?.closeDrawerGestureModeMask = .panningCenterView

mainContainer?.setGestureCompletionBlock({ (drawer, gesture) in
    if drawer?.openSide != .left {
        print("Drawer Closed")
    }else {
        print("Drawer Opened")
    }
})

window?.rootViewController = mainContainer
window?.makeKeyAndVisible()

return true

}

You may have to ensure that you set

mainContainer?.openDrawerGestureModeMask = .panningCenterView
mainContainer?.closeDrawerGestureModeMask = .panningCenterView

and check if drawer's openSide is left else its closed.

Objective-C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    UIViewController *leftDrawer = [[UIViewController alloc] init];
    [[leftDrawer view] setBackgroundColor:[UIColor redColor]];
    UIViewController *centerDrawer = [[UIViewController alloc] init];
    [[centerDrawer view] setBackgroundColor:[UIColor greenColor]];

    MMDrawerController *mainDrawer = [[MMDrawerController alloc] initWithCenterViewController:centerDrawer leftDrawerViewController:leftDrawer];
    mainDrawer.openDrawerGestureModeMask = MMOpenDrawerGestureModePanningCenterView;
    mainDrawer.closeDrawerGestureModeMask = MMOpenDrawerGestureModePanningCenterView;

    [mainDrawer setGestureCompletionBlock:^(MMDrawerController *drawerController, UIGestureRecognizer *gesture) {
        if (drawerController.openSide != MMDrawerSideLeft) {
            NSLog(@"Drawer Closed");
        }else{
            NSLog(@"Drawer Opened");
        }
    }];

    self.window.rootViewController = mainDrawer;
    [self.window makeKeyAndVisible];

    return YES;
}

Upvotes: 2

Related Questions