0

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

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

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

谁能解释我如何检测菜单已关闭?(根据给定的答案或如果您有另一个答案)

编辑:

在情节提要中,我有一个 UINavigationController 链接到一个 UIViewController,其类型为 MMDrawerController( myVCMMdrawerController),然后我还有myCenterVC,leftVCrightVC. 在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];

myCenterVC我进行操作以在 IBActions 中显示菜单,如下所示:

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

1 回答 1

2

这是对我有用的代码:

斯威夫特 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关闭。

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;
}
于 2017-07-03T16:24:07.727 回答