使用MMDrawerController检测抽屉何时关闭

时间:2015-09-11 08:24:26

标签: ios swift mmdrawercontroller

有什么方法可以通过拖动中心容器来检测抽屉何时关闭?我希望每次关闭抽屉时都要执行某些操作..

4 个答案:

答案 0 :(得分:2)

您可以使用

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

设置回调块以在手势完成时收到通知。在此块中,查询drawerController以了解当前打开的内容。

/**
 Sets a callback to be called when a gesture has been completed.

 This block is called when a gesture action has been completed. You can query the `openSide` of the `drawerController` to determine what the new state of the drawer is.

 @param gestureCompletionBlock A block object to be called that allows the implementer be notified when a gesture action has been completed.
 */

答案 1 :(得分:2)

我认为我们可以使用以下辅助方法来检测MMDrawerController状态。

对于Objective-C

- (BOOL)isLeftOpen {
    return (self.mm_drawerController.openSide == MMDrawerSideLeft);
}

- (BOOL)isRightOpen {
    return (self.mm_drawerController.openSide == MMDrawerSideRight);
}

适用于Swift-3.x

func isLeftOpen() -> Bool {
    return mm_drawerController.openSide == .left
}

func isRightOpen() -> Bool {
    return mm_drawerController.openSide == .right
}. 

来源: https://github.com/mutualmobile/MMDrawerController/issues/337

答案 2 :(得分:0)

使用Quentin的答案,您可以检查手势完成时的抽屉宽度

    [self setGestureCompletionBlock:^(MMDrawerController *drawerController, UIGestureRecognizer *gesture) {
    if (drawerController.visibleLeftDrawerWidth == 0.0f) {
        // "perform certain actions"
    }
}];

答案 3 :(得分:0)

只要抽屉打开和关闭,您就可以收到回电信息。

drawerController?.setDrawerVisualStateBlock({ (drawer, drawerSide, percentVisible) in
                print(percentVisible)
                if percentVisible > 0 && self.blackView.superview == nil {
                    //drawer opened
                                }
                                if percentVisible == 0 {
                                    //drawer closed
                                }
            })

注意:当抽屉关闭时,percentVisible为0,抽屉打开时为1。