iOS开发之如何实现“抽屉”效果 您所在的位置:网站首页 ios效果量 iOS开发之如何实现“抽屉”效果

iOS开发之如何实现“抽屉”效果

2024-07-14 05:34| 来源: 网络整理| 查看: 265

iOS开发之如何实现“抽屉”效果

现在基本上每一个App中左划都会出现一个页面,基本上都是只占主页面的一部分,效果就像是一个抽屉一样。最近在写项目时,关于如何达到抽屉效果,总结了一些东西。 先看看效果图: 在这里插入图片描述

实现过程

首先我们需要去创建一个新的视图控制器,让它作为我们的要实现的抽屉的根视图,在此视图控制器我们要添加对应的左视图,要是需要右视图也可以添加,然后设定方法:

@property (nonatomic, strong) UIViewController *rootViewController; //左侧视图 @property (nonatomic, strong) UIViewController *leftViewController; //菜单宽度 @property (nonatomic, assign, readonly) CGFloat menuWidth; //留白宽度 @property (nonatomic, assign, readonly) CGFloat emptyWidth; //是否允许滚动 @property (nonatomic ,assign) BOOL slideEnabled; //创建方法 -(instancetype)initWithRootViewController:(UIViewController*)rootViewController; //显示主视图 -(void)showRootViewControllerAnimated:(BOOL)animated; //显示左侧菜单 -(void)showLeftViewControllerAnimated:(BOOL)animated;

接着我们将定义的方法进行实现:

-(instancetype)initWithRootViewController:(UIViewController*)rootViewController{ if (self = [super init]) { _rootViewController = rootViewController; [self addChildViewController:_rootViewController]; [self.view addSubview:_rootViewController.view]; [_rootViewController didMoveToParentViewController:self]; } return self; } - (void)showLeftViewControllerAnimated:(BOOL)animated { if (!_leftViewController) {return;} [self.view sendSubviewToBack:_rightViewController.view]; _coverView.hidden = false; [_rootViewController.view bringSubviewToFront:_coverView]; [UIView animateWithDuration:[self animationDurationAnimated:animated] animations:^{ _rootViewController.view.center = CGPointMake(_rootViewController.view.bounds.size.width/2 + self.menuWidth, _rootViewController.view.center.y); _leftViewController.view.frame = CGRectMake(0, 0, [self menuWidth], self.view.bounds.size.height); _coverView.alpha = MaxCoverAlpha; }]; }

然后我们需要添加一个分类,让它向前声明新的视图控制器,添加一个创建视图的方法使用懒加载:

- (XLSlideMenuViewController *)xl_sldeMenu { UIViewController *sldeMenu = self.parentViewController; while (sldeMenu) { if ([sldeMenu isKindOfClass:[XLSlideMenuViewController class]]) { return (XLSlideMenuViewController *)sldeMenu; } else if (sldeMenu.parentViewController && sldeMenu.parentViewController != sldeMenu) { sldeMenu = sldeMenu.parentViewController; } else { sldeMenu = nil; } } return nil; }

然后我们在使用抽屉的时候,需要西安去设置根视图,然后将左侧视图初始化并将左视图添加在前边设置好的左视图属性上:

UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:tabBarControllerTest]; nav.modalPresentationStyle = UIModalPresentationFullScreen; LeftViewController *leftVC = [[LeftViewController alloc] init]; XLSlideMenuViewController *slideMenu = [[XLSlideMenuViewController alloc] initWithRootViewController:nav]; slideMenu.leftViewController = leftVC; self.window.rootViewController = slideMenu; slideMenu.modalPresentationStyle = UIModalPresentationFullScreen; [self presentViewController:slideMenu animated:NO completion:nil];

最后在我们还可以添加点击事件,并且添加拖拽方法,使操作更加简单:

-(void)panChanged:(UIPanGestureRecognizer*)pan{ //拖拽的距离 CGPoint translation = [pan translationInView:self.view]; //移动主控制器 _rootViewController.view.center = CGPointMake(_originalPoint.x + translation.x, _originalPoint.y); //判断是否设置了左右菜单 if (!_rightViewController && CGRectGetMinX(_rootViewController.view.frame) _rootViewController.view.frame = self.view.bounds; } //滑动到边缘位置后不可以继续滑动 if (CGRectGetMinX(_rootViewController.view.frame) > self.menuWidth) { _rootViewController.view.center = CGPointMake(_rootViewController.view.bounds.size.width/2 + self.menuWidth, _rootViewController.view.center.y); } if (CGRectGetMaxX(_rootViewController.view.frame) //显示左菜单 [self.view sendSubviewToBack:_rightViewController.view]; //更新左菜单位置 [self updateLeftMenuFrame]; //更新遮罩层的透明度 _coverView.hidden = false; [_rootViewController.view bringSubviewToFront:_coverView]; _coverView.alpha = CGRectGetMinX(_rootViewController.view.frame)/self.menuWidth * MaxCoverAlpha; }else if (CGRectGetMinX(_rootViewController.view.frame)


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有