用MMDrawerController实现侧滑

楚茂实
2023-12-01

我这里使用的是cocoapods来安装的MMDrawerController库,细节不再详述。

CenterViewController

在加载本视图控制器的时候,由于其它视图控制器的切换可能会关闭 通过手势滑动打开抽屉的模式,因此需要设置一下:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
}
  1. 首先给导航栏添加按钮
    UIBarButtonItem* leftBarBtnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(pressLeftBtn)];
    self.navigationItem.leftBarButtonItem = leftBarBtnItem;

    UIBarButtonItem* rightBarBtnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(pressRightBtn)];
    self.navigationItem.rightBarButtonItem = rightBarBtnItem;
  1. 添加双击手势
    UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
    //单手指双击
    [doubleTap setNumberOfTapsRequired:2];
    [self.view addGestureRecognizer:doubleTap];

    //双手指双击
    UITapGestureRecognizer * twoFingerDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerDoubleTap:)];
    [twoFingerDoubleTap setNumberOfTapsRequired:2];
    //两个手指  默认为一个
    [twoFingerDoubleTap setNumberOfTouchesRequired:2];
    [self.view addGestureRecognizer:twoFingerDoubleTap];
  1. 实现按钮的事件函数
-(void)pressLeftBtn
{
     //self.mm_drawerController通过遍历循环拿到之前在AppDelegate中声明的那个MMDrawerController属性,然后判断是否为打开状态,如果是就关闭,否就是打开
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}
-(void)pressRightBtn
{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideRight animated:YES completion:nil];
}
  1. 实现双击屏幕的事件函数
/*
边缘反弹
*/

//单手双击
-(void)doubleTap:(UITapGestureRecognizer*) gesture
{
    [self.mm_drawerController bouncePreviewForDrawerSide:MMDrawerSideLeft completion:nil];
}

//双手指双击
-(void)twoFingerDoubleTap:(UITapGestureRecognizer*) gesture
{
    [self.mm_drawerController bouncePreviewForDrawerSide:MMDrawerSideRight completion:nil];
}

LeftViewController | RightViewController

  1. 实现 UITableViewDataSourceUITableViewDelegate 的相关协议函数
  2. 当选中tableView的cell后,跳转到 ShowViewControll 视图控制器中
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LitterLShowViewController *showVC = [[LitterLShowViewController alloc]init];
    showVC.title = [NSString stringWithFormat:@"Left-Demo%ld",indexPath.row];

    //拿到我们的LitterLCenterViewController,让它去push
    UINavigationController* nav = (UINavigationController*)self.mm_drawerController.centerViewController;
    [nav pushViewController:showVC animated:NO];

    //当我们push成功之后,关闭我们的抽屉
    [self.mm_drawerController closeDrawerAnimated:YES completion:^(BOOL finished) {
        //设置打开抽屉模式为MMOpenDrawerGestureModeNone,也就是没有任何效果。
        [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
    }];
}

需要注意的是,push成功之后,必须要关闭掉我们的抽屉效果。否则,显示ShowViewController的时候仍然是抽屉的样式

ShowViewController

  • 在该视图控制器中只设置了背景色为白色。

  • 选中cell push进入ShowViewController之后,当你轻轻往右滑动的时候 其实它已经加载了centerViewController中的viewWillAppear这个方法,这个方法打开了通过手势滑动打开抽屉的模式 ,因此导致没有回到centerViewController,而显示的是抽屉模式。你在ShowViewController的viewWillAppear这个方法中加上关闭通过手势滑动打开抽屉的模式就可以了

AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    //1、初始化控制器
    UIViewController* centerVC = [[LitterLCenterViewController alloc]init];
    UIViewController* leftVC = [[LitterLLeftViewController alloc]init];
    UIViewController* rightVC = [[LitterLRightViewController alloc]init];
    //2、初始化导航控制器
    UINavigationController* centerNvaVC = [[UINavigationController alloc]initWithRootViewController:centerVC];
    UINavigationController* leftNvaVC = [[UINavigationController alloc]initWithRootViewController:leftVC];
    UINavigationController* rightNvaVC = [[UINavigationController alloc]initWithRootViewController:rightVC];

    //3、使用MMDrawerController
    self.drawerController = [[MMDrawerController alloc]initWithCenterViewController:centerNvaVC leftDrawerViewController:leftNvaVC rightDrawerViewController:rightNvaVC];

    //4、设置打开/关闭抽屉的手势
    self.drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
    self.drawerController.closeDrawerGestureModeMask =MMCloseDrawerGestureModeAll;
    //5、设置左右两边抽屉显示的多少
    self.drawerController.maximumLeftDrawerWidth = 300.0;
    self.drawerController.maximumRightDrawerWidth = 200.0;


    //6、初始化窗口、设置根控制器、显示窗口
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.window setRootViewController:self.drawerController];
    [self.window makeKeyAndVisible];

    return YES;
}

总体上大致流程就是如此了!


完整项目

转载自 http://www.jianshu.com/p/9e55cbf7d5ab

 类似资料: