MMDrawerController侧滑demo

作者: 迷路的安然和无恙 | 来源:发表于2016-07-04 00:29 被阅读839次

首先看下效果图

侧滑Demo.gif

MMDrawerController固然好用,但如果你遇到了和我相同的问题,那就是,当push出的子控制器,左划屏幕的时候也能显示菜单栏,而不是像QQ的侧边栏那样炫酷的效果,那你可以继续往下看我的代码。

AppDelegate中写下以下代码


#import "LeftViewController.h"
#import "RightViewController.h"
#import <MMDrawerController.h>
#import "NavViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //创建窗口
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    //创建左侧控制器
    LeftViewController *leftVc = [LeftViewController new];
    
    //创建右侧控制器
    RightViewController *rightVc = [RightViewController new];
    
    //中间控制器
    MMDrawerController *drawerVc = [[MMDrawerController alloc]initWithCenterViewController:rightVc leftDrawerViewController:leftVc];
    
    //开启侧滑
    [drawerVc setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    
    //关闭侧滑
    [drawerVc setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    
    //创建导航控制器 并将drawer控制器设为根控制器
    NavViewController *nav = [[NavViewController alloc]initWithRootViewController:drawerVc];
    
    //将导航控制器设为window的根控制器
    self.window.rootViewController = nav;

    //显示
    [self.window makeKeyAndVisible];
    
    return YES;
}

左侧滑控制器中写上

#pragma mark - 视图即将出现

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置背景色
    self.view.backgroundColor = [UIColor whiteColor];
    
    //加载tableViewHeader xib
    self.tableView.tableHeaderView = [Header headerLoad];
    
    //禁止滚动
    self.tableView.scrollEnabled = NO;
    
}

#pragma mark - 数据源方法
//确定行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 10;
    
}

//确定cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = @"小林子";
    
    return cell;
}

#pragma mark - 选中跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //创建子控制器
    TestViewController *testVc = [TestViewController new];
    
    //跳转
    [self.navigationController pushViewController:testVc animated:YES];

}

中间视图


#import "RightViewController.h"
#import <UIViewController+MMDrawerController.h>
#import <Masonry.h>
#import "TestViewController.h"
@interface RightViewController ()

@end

@implementation RightViewController

#pragma mark - 隐藏导航控制器
- (void)viewWillAppear:(BOOL)animated {
    //加载界面是隐藏导航控制器
    [self.navigationController setNavigationBarHidden:YES];
    
}

#pragma mark - 显示导航控制器
- (void)viewWillDisappear:(BOOL)animated {
    //界面即将消失时显示导航控制器
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置背景色
    self.view.backgroundColor = [UIColor blueColor];
    
    //自定义中间导航控制器
    [self custumView];
}

#pragma mark - 自定center控制器的navigationBar

- (void)custumView {
    
    //自定义center控制器的导航控制器的view
    UIView *viewBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    
    //设置navigationBar的背景图片
    viewBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"anniu"]];
    
    //左侧按钮
    UIButton *leftButton = [[UIButton alloc]initWithFrame:CGRectMake(15, 30,24, 24)];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"u73"] forState:
     UIControlStateNormal];
    
    //设置圆角
    [leftButton setBackgroundColor:[UIColor whiteColor]];
    leftButton.layer.cornerRadius = 12;
    leftButton.layer.masksToBounds = YES;
    //添加事件
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    //添加
    [viewBar addSubview:leftButton];
    
    //右侧按钮
    UIButton *rightButton = [[UIButton alloc]init];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"u73"] forState:UIControlStateNormal];
    
    //设置圆角
    [rightButton setBackgroundColor:[UIColor whiteColor]];
    rightButton.layer.cornerRadius = 10;
    rightButton.layer.masksToBounds = YES;
    
    [rightButton addTarget:self action:@selector(rightItemClick) forControlEvents:UIControlEventTouchUpInside];
    //添加
    [viewBar addSubview:rightButton];
    
    //设置右侧按钮的约束
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewBar).with.offset(30);
        make.right.equalTo(viewBar).with.offset(-15);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    //添加首页标签
    UILabel *titleLabel = [[UILabel alloc]init];
    
    titleLabel.text = @"测试";
    
    [titleLabel setTextColor:[UIColor whiteColor]];
    
    titleLabel.font = [UIFont systemFontOfSize:18];
    [viewBar addSubview:titleLabel];
    
    //添加约束
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewBar).with.offset(22);
        make.centerX.equalTo(viewBar);
        make.size.mas_equalTo(CGSizeMake(80, 40));
    }];
    //添加
    [self.view addSubview:viewBar];
    
}

#pragma mark - 点击收回/打开MMDrawerSide
- (void)back{
    //判断是否打开Side
    if (!self.mm_drawerController.openSide) {
        //打开
        [self.mm_drawerController openDrawerSide:MMDrawerSideLeft animated:YES completion:^(BOOL finished) {
        }];
    }
    //关闭
    [self.mm_drawerController closeDrawerAnimated:YES completion:^(BOOL finished) {
        
    }];
}
#pragma mark - 右侧按钮点击事件
- (void)rightItemClick {
    
    //创建子控制器
    TestViewController *testVc = [TestViewController new];
    
    //跳转
    [self.navigationController pushViewController:testVc animated:YES];
}

总结

因为是演示代码所以看起来有点多,实现思路其实是这样:

  • 创建左侧控制器和center控制器
  • 创建MMDrawer控制器 将左侧控制器和center控制器设置给MMDrawer控制器
  • 创建导航控制器,将导航控制器的根控制器设置为MMDrawer
  • 将导航控制器设置为weindow的跟控制器
  • 隐藏center的导航栏,然后创建一个View添加到View上占据导航栏的位置,添加按钮实现点击事件即可。
    如果你感兴趣的话,请关注我或者到我的github上下载,下载完成后需要Cocopods集成Masonry和MMDrawerController。

相关文章

网友评论

    本文标题:MMDrawerController侧滑demo

    本文链接:https://www.haomeiwen.com/subject/nbecjttx.html