本文介绍: iOS开发如何实现抽屉效果现在基本上每一个App中左划都会出现一个页面基本上都是只占主页面的一部分效果就像是一个抽屉一样。最近在写项目时,关于如何达到抽屉效果总结了一些东西。先看看效果图实现过程首先我们需要创建一个新的视图控制器,让它作为我们的要实现的抽屉的根视图,在此视图控制我们添加对应的左视图,要是需要视图可以添加然后设定方法:@property (nonatomic, strong) UIViewController *rootViewController;//

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 &amp;&amp; CGRectGetMinX(_rootViewController.view.frame) <= 0 ) {
        _rootViewController.view.frame = self.view.bounds;
    }
    if (!_leftViewController &amp;&amp; CGRectGetMinX(_rootViewController.view.frame) >= 0) {
        _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.emptyWidth) {
        _rootViewController.view.center = CGPointMake(_rootViewController.view.bounds.size.width/2 - self.menuWidth, _rootViewController.view.center.y);
    }
    //判断显示左菜单还是右菜单
    if (CGRectGetMinX(_rootViewController.view.frame) > 0) {
        //显示左菜单
        [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) < 0){
        
        //更新遮罩层的透明
        _coverView.hidden = false;
        [_rootViewController.view bringSubviewToFront:_coverView];
        _coverView.alpha = (CGRectGetMaxX(self.view.frame) - CGRectGetMaxX(_rootViewController.view.frame))/self.menuWidth * MaxCoverAlpha;
    }
}

然后左视图中的具体内容我们就可以自己设置了,这样就达到了一个“抽屉”的效果。

原文地址:https://blog.csdn.net/weixin_51638861/article/details/123030324

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_25430.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注