iOS 抽屉效果 ViewDeck

抽屉效果目前比较有名的有第三方RESideMenuMMDrawerController。但是项目要求抽屉效果为拉出形式,并且要有黑色透明遮罩层,然后在GitHub发现了一个更好用的库ViewDeck

ViewDeck

  • 支持左边和右边侧边栏
  • 抽屉拉出效果,带有黑色透明遮罩层
  • 支持点击和滑动打开、关闭侧边栏
  • 侧边栏显示内容尺寸可以控制
  • 省心省力,简洁好用,快速集成

目前侧滑关闭功能仅在示例demo里,作者并未发布到cocoapods issues。通过pods方式导入的并没有侧滑关闭功能,如需侧滑功能,请手动导入本demo或GitHub demo ViewDeck文件夹 —20190105

ViewDeck抽屉效果.gif
1.配置侧边栏
    UITabBarController *tarBarCtr=[[UITabBarController alloc]init];
    [tarBarCtr setViewControllers:[NSArray arrayWithObjects:centNvi,centSecNvi, nil]];
    _rootTabbarCtrV=tarBarCtr;
    //左边view 采用xib多种形式研究
    LeftSideViewController *leftVC =[[LeftSideViewController alloc]initWithNibName:@"LeftSideViewController" bundle:[NSBundle mainBundle]];
    //右边
    RightSideViewController *rightView=[[RightSideViewController alloc]init];
    UINavigationController *rightNvi=[[UINavigationController alloc]initWithRootViewController:rightView];
    //配置侧边栏
    IIViewDeckController *viewDeckController =[[IIViewDeckController alloc]initWithCenterViewController:_rootTabbarCtrV leftViewController:leftVC rightViewController:rightNvi];
    //只有左侧边栏
//  IIViewDeckController *viewDeckController =[[IIViewDeckController alloc]initWithCenterViewController:_rootTabbarCtrV leftViewController:leftVC];
    viewDeckController.delegate=self;
    self.window.rootViewController=viewDeckController;
2.首页点击侧边栏按钮和跳转下一页

通过openSide:(IIViewDeckSide)方法可以左、右侧边栏; panningEnabled属性仅可以控制滑动打开关闭,但点击关闭和openSide:打开是不在这个属性的控制范围内的

typedef NS_ENUM(NSInteger, IIViewDeckSide) {
    IIViewDeckSideNone = 0, /// This identifies no side, basically meaning that neither the left nor the right side is relevant.
    IIViewDeckSideLeft, /// This identifies the left view controller of an IIViewDeckController
    IIViewDeckSideRight, /// This identifies the right view controller of an IIViewDeckController

    IIViewDeckSideUnknown = IIViewDeckSideNone, /// This has the same logic as IIViewDeckSideNone but means that the side is yet unknown.

    IIViewDeckLeftSide __deprecated_enum_msg("Use IIViewDeckSideLeft instead.") = IIViewDeckSideLeft,
    IIViewDeckRightSide __deprecated_enum_msg("Use IIViewDeckSideRight instead.") = IIViewDeckSideRight,
};

//点击打开左侧侧边栏
-(void)actionOfTapLeftEvent{
    [self.viewDeckController openSide:IIViewDeckSideLeft animated:YES];
}
//跳转下一页
-(void)actionOfTapRightEvent{
    InfoViewController *infoView=[[InfoViewController alloc]init];
    [infoView setHidesBottomBarWhenPushed:YES];
//不需要类似RESideMenu把panGestureEnabled关闭
    [self.navigationController pushViewController:infoView animated:YES];
}
3.侧边栏显示内容宽度设置和跳转

通过preferredContentSize属性设置侧边栏的内容宽度尺寸

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   //ViewDeck默认高度始终是视图控制器本身的高度上
    self.preferredContentSize = CGSizeMake(ScreenWidth/3*2, ScreenHeight);
    self.view.backgroundColor=[UIColor whiteColor];
    
}
- (IBAction)goNextView:(UIButton *)sender {
    /**
     关闭侧边栏
     */
    [self.viewDeckController closeSide:YES];
    UINavigationController *navCtr= ((AppDelegate*)[UIApplication sharedApplication].delegate).rootTabbarCtrV.selectedViewController;
    InfoViewController *infoView=[[InfoViewController alloc]init];
    [infoView setHidesBottomBarWhenPushed:YES];
    [navCtr pushViewController:infoView animated:YES];
}
4.打开关闭IIViewDeckControllerDelegate代理

可以通过代理控制某些情况下才能打开侧边栏等情况,也可以做些打开关闭时的相关操作

//即将打开侧边栏。 return NO 侧滑无法打开,但通过openSide:方法可以强行打开;
- (BOOL)viewDeckController:(IIViewDeckController *)viewDeckController willOpenSide:(IIViewDeckSide)side;
//已经打开侧边栏。可以做一些后续操作
- (void)viewDeckController:(IIViewDeckController *)viewDeckController didOpenSide:(IIViewDeckSide)side;
//即将关闭
- (BOOL)viewDeckController:(IIViewDeckController *)viewDeckController willCloseSide:(IIViewDeckSide)side;
//即将打开
- (void)viewDeckController:(IIViewDeckController *)viewDeckController didCloseSide:(IIViewDeckSide)side;
4.关闭侧边栏时,可滑动关闭

IIViewDeckController.mm 源码文件中,decorationTapGestureRecognizer、dismissGestureRecognizer是关闭侧边栏的手势,其中一个是点击关闭一个是新加的滑动关闭

@property (nonatomic) UIScreenEdgePanGestureRecognizer *leftEdgeGestureRecognizer;
@property (nonatomic) UIScreenEdgePanGestureRecognizer *rightEdgeGestureRecognizer;
@property (nonatomic) UITapGestureRecognizer *decorationTapGestureRecognizer;
//滑动关闭侧边栏
@property (nonatomic) UIPanGestureRecognizer *dismissGestureRecognizer;

下面这个库是个古董级的,不过功能齐全,实际使用除了个别需要注意外也还不错。欢迎体验: REFrostedViewController

REFrostedViewController

  • 侧边栏显示内容尺寸可以控制
  • 抽屉拉出效果,带有黑色透明遮罩层
  • 可以侧滑关闭(重点)
  • 缺点:仅支持单边侧边栏(左边或右边)
  • 缺点:显示侧边栏时需要手动实现滑动手势,会有隐藏隐患
  • 缺点:拉出和关闭时,黑色遮罩并不完美
  • 缺点:已经停止维护了三四年了(老古董了)
1.配置侧边栏
    //侧边栏
    REFMenuViewController *menuView = [[REFMenuViewController alloc]init];
   REFrostedViewController *rostedViewController = [[REFrostedViewController alloc] initWithContentViewController:tarBarCtr menuViewController:menuView];
    rostedViewController.direction = REFrostedViewControllerDirectionLeft;
    rostedViewController.liveBlurBackgroundStyle = REFrostedViewControllerLiveBackgroundStyleDark;
    rostedViewController.liveBlur = YES;
    rostedViewController.limitMenuViewSize = YES;
    rostedViewController.backgroundFadeAmount=0.5;
    rostedViewController.delegate = self;
    rostedViewController.menuViewSize=CGSizeMake(leftSideMeunWidth, ScreenHeight);
2.首页点击侧边栏按钮和跳转下一页
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"首页";
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithTitle:@"左边栏" style:UIBarButtonItemStylePlain target:self action:@selector(actionOfTapLeftEvent:)];
    self.navigationItem.leftBarButtonItem=leftBarButton;
    UIBarButtonItem *rightBarButton=[[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(actionOfTapRightEvent)];
    self.navigationItem.rightBarButtonItem=rightBarButton;
    /**
     如需要侧滑显示侧边栏,则要实现滑动手势
     */
    [self.view addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognized:)]];
    [self creatVC];
}
//点击显示侧边栏
-(void)actionOfTapLeftEvent:(UIButton *)sender{
    [self.frostedViewController presentMenuViewController];
}
//滑动显示侧边栏
- (void)panGestureRecognized:(UIPanGestureRecognizer *)sender{
    [self.frostedViewController panGestureRecognized:sender];
}
3.侧边栏关闭和跳转
//跳转下一页
-(void)actionOfTapRightEvent{
    /**
     关闭右侧侧边栏
     */
    [self.frostedViewController hideMenuViewController];
    
    UINavigationController *navCtr= ((AppDelegate*)[UIApplication sharedApplication].delegate).rootTabbarCtrV.selectedViewController;
    REFInfoViewController *infoView=[[REFInfoViewController alloc]init];
    [infoView setHidesBottomBarWhenPushed:YES];
    [navCtr pushViewController:infoView animated:YES];
}
4.当视图内含有UIScrollView时,会导致侧滑失效,解决方案之一可以继承UIScrollView并实现如下:
/**
 *  重写手势,如果是左滑,则禁用掉scrollview自带的;右滑很少出现,此处未实现
 */
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
    {
        UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
        if([pan translationInView:self].x > 0.0f && self.contentOffset.x == 0.0f)
        {
            return NO;
        }
    }
    return [super gestureRecognizerShouldBegin:gestureRecognizer];
}

最后小demo附上:ViewDeckStudy 内含ViewDeck和REFrostedViewController使用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,458评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,454评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,171评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,062评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,440评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,661评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,906评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,609评论 0 200
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,379评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,600评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,085评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,409评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,072评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,088评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,860评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,704评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,608评论 2 270

推荐阅读更多精彩内容