欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

iOS开发实现抽屉效果,

来源: 开发者 投稿于  被查看 19891 次 评论:34

iOS开发实现抽屉效果,


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) <= 0 ) {
        _rootViewController.view.frame = self.view.bounds;
    }
    if (!_leftViewController && 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;
    }
}

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持3672js教程。

您可能感兴趣的文章:
  • iOS开发之路--仿网易抽屉效果
  • IOS中MMDrawerController第三方抽屉效果的基本使用示例
  • iOS实现简单的抽屉效果
  • iOS实现侧拉栏抽屉效果
  • IOS中Swift仿QQ最新版抽屉侧滑和弹框视图
  • iOS实现左右拖动抽屉效果
  • IOS实现点击滑动抽屉效果
  • ios仿侧边抽屉效果实现代码
  • iOS实现简单抽屉效果
  • iOS实现简易抽屉效果、双边抽屉效果

用户评论