0

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];
Ne AS
  • 1,490
  • 3
  • 26
  • 58

1 Answers1

2

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;
}
Lawrence Tan
  • 507
  • 2
  • 12
  • can you please provide the `objective-c` version? – Ne AS Jul 03 '17 at 16:53
  • Thank you! What if I create the drawer in an UIViewController? In the App Delegate I have created MMDrawerController *mainDrawer as a property, then in my UIViewController I set this mainDrawer with the one in the UIViewController. But it seems that the mainDrawer is not set. Have you an idea about this problem please? Thanks again – Ne AS Jul 04 '17 at 08:31
  • Can you provide code snippets of what you do to help me understand the problem please? @Llg – Lawrence Tan Jul 04 '17 at 09:09
  • @Llg is there any good reason why you want to set it in another uiviewcontroller? its a common practise to initialise and set at the same time when the drawer controller is loaded into memory. – Lawrence Tan Jul 04 '17 at 17:12
  • Thank you, Lawrence Tan. Actually I was searching for the same task. And this is working great. – RajibTheKing Oct 24 '17 at 19:04