MMDrawerController:检测菜单关闭动作或滑动手势

时间:2017-07-03 15:55:07

标签: ios objective-c mmdrawercontroller

我想检测菜单何时关闭。我发现这个SO解决了question。在接受的答案的评论中,他们说这种方法:

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

必须位于App delegate才能在菜单关闭时收到通知。我把这个函数放在我的App委托中,在它的实现中我创建了一个NSLog(@"menu closed");,但是在我的控制台中没有打印任何内容,当我关闭菜单时函数没有被触发。

任何人都可以解释我,我怎么能检测到菜单已关闭? (根据给定的答案或如果你有另一个答案)

修改

在故事板中,我有一个UINavigationController链接到一个UIViewController,其类型为MMDrawerController(myVCMMdrawerController),然后我还有myCenterVCleftVC和{{1} }。在rightVC myVCMMdrawerController这就是我的工作:

viewDidLoad

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]; 中我执行操作以在IBActions中显示菜单,如下所示:

myCenterVC

1 个答案:

答案 0 :(得分:2)

这里有适合我的代码:

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

}

您可能必须确保设置

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

并检查drawer's openSide is left是否已关闭。

<强>目标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;
}