问题
Is there any way we can detect when the drawer is closed by dragging the center container? I wish to perform certain actions everytime the drawer is closed..
回答1:
You can use
-(void)setGestureCompletionBlock:(void(^)(MMDrawerController * drawerController, UIGestureRecognizer * gesture))gestureCompletionBlock;
to set a callback block to be notified when a gesture is completed. In this block, query the drawerController to know what is currently open.
/**
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.
*/
回答2:
I think we can use following helper methods to detect MMDrawerController state.
For Objective-C
- (BOOL)isLeftOpen {
return (self.mm_drawerController.openSide == MMDrawerSideLeft);
}
- (BOOL)isRightOpen {
return (self.mm_drawerController.openSide == MMDrawerSideRight);
}
For Swift-3.x
func isLeftOpen() -> Bool {
return mm_drawerController.openSide == .left
}
func isRightOpen() -> Bool {
return mm_drawerController.openSide == .right
}.
Source: https://github.com/mutualmobile/MMDrawerController/issues/337
回答3:
Using Quentin's answer you can check for the drawer width on gesture completion as
[self setGestureCompletionBlock:^(MMDrawerController *drawerController, UIGestureRecognizer *gesture) {
if (drawerController.visibleLeftDrawerWidth == 0.0f) {
// "perform certain actions"
}
}];
回答4:
You can get a callback whenever the drawer will be open and closed.
drawerController?.setDrawerVisualStateBlock({ (drawer, drawerSide, percentVisible) in
print(percentVisible)
if percentVisible > 0 && self.blackView.superview == nil {
//drawer opened
}
if percentVisible == 0 {
//drawer closed
}
})
Note: percentVisible will be 0 when drawer will be closed and 1 when the drawer will be opened.
来源:https://stackoverflow.com/questions/32518879/detect-when-the-drawer-is-closed-while-using-mmdrawercontroller