Giter VIP home page Giter VIP logo

hysideviewcontroller's Introduction

HYSideViewController

#####导读

左右侧滑是App开发中非常常见的功能,目前大部分App都有实现这个功能.

为提高开发效率,笔者对左右侧滑功能进行了封装.代码简洁,思路清晰易懂,外界只需调用两个接口,即可实现左右侧滑功能!!!

####简书链接http://www.jianshu.com/p/7e314fe9c403


#####简单介绍一下这个侧滑的使用

1-> 侧滑并设置侧滑动画时间

    /** 侧滑并设置侧滑动画时间 */
- (void)sideAnimateDuration:(NSTimeInterval)duration SideDirection:(HYSideDirection)sideDirectionType;

2-> 设置侧滑控制器,可只实现一侧,也可以都实现

/** 设置左侧的控制器 */
- (void)setLeftSideVC:(UIViewController *)leftSideVC;
/** 设置右侧的控制器 */
- (void)setRightSideVC:(UIViewController *)rightSideVC;

####效果图

1.gif

同时集成左滑和右滑

2.gif


####实现思路及主要代码详解

######思路简述 把将要侧滑出来的View添加到[UIApplication sharedApplication].keyWindow上,并用一个控制器来对这个View进行管理.如果是向右滑动,则添加到左侧.向右滑动时,将keyWindow上的所有子控件进行向右平移.我们想要的侧滑效果即可出来了.

######主要代码 1.外界通过调用setter方法,添加侧滑控制器

- (void)setLeftSideVC:(UIViewController *)leftSideVC{
    
    _leftSideVC = leftSideVC;
    [[UIApplication sharedApplication].keyWindow addSubview:self.leftSideView];
    CGRect rect = self.leftSideView.bounds;
    CGFloat sideWidth = self.leftSideView.bounds.size.width;
    sideWidth = rect.size.width;
    self.leftSideView.frame = CGRectMake(- rect.size.width, 0, rect.size.width, [UIScreen mainScreen].bounds.size.height);
}

- (void)setRightSideVC:(UIViewController *)rightSideVC{
    
    _rightSideVC = rightSideVC;
    [[UIApplication sharedApplication].keyWindow addSubview:self.rightSideView];
    CGRect rect = self.rightSideView.bounds;
    CGFloat sideWidth = self.rightSideView.bounds.size.width;
    sideWidth = rect.size.width;
    self.rightSideView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width , 0 , rect.size.width, [UIScreen mainScreen].bounds.size.height);
}

2.外界调用侧滑接口,侧滑并设置侧滑动画时间.并相应的调整状态栏样式.

- (void)sideAnimateDuration:(NSTimeInterval)duration SideDirection:(HYSideDirection)sideDirectionType{
    
        //修改状态栏样式
    if([UIApplication sharedApplication].statusBarStyle == UIStatusBarStyleDefault){
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    }else{
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    }
    if (self.isSide) {
        self.isSide = NO;
        [UIView animateWithDuration:duration animations:^{
            for (UIView * view in [UIApplication sharedApplication].keyWindow.subviews) {
                view.transform = CGAffineTransformIdentity;
            }
        }completion:^(BOOL finished) {

            [self hiddenSideView];
        }];
        return;
    }
    
    self.isSide = YES;
    if (sideDirectionType == HYSideDirectionLeft){
        self.rightSideView.hidden = NO;
    }else{
        self.leftSideView.hidden = NO;
    }
    CGFloat _sideWidth = (sideDirectionType == HYSideDirectionRight) ? self.leftSideView.frame.size.width : - self.rightSideView.frame.size.width;
    [UIView animateWithDuration:duration animations:^{
        [self sideDistance:_sideWidth];
    }];
}

3.核心的代码是滑动手势的回调方法,滑动时,根据滑动方向,滑动距离两个条件做相应的事件处理.滑动停止时,同样根据滑动方向,滑动距离两个条件,来判断是否需要将View侧滑出来.

  • 手势滑动时

手势滑动.png

  • 手势停止时 手势停止.png

####简书链接http://www.jianshu.com/p/7e314fe9c403

hysideviewcontroller's People

Contributors

helloyeah avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

hysideviewcontroller's Issues

几个小问题

我遇到个问题,我使用源代码里isSide属性作为判断侧边栏状态的值,不过遇到个很诡异的问题.
1.集成到项目中后,isSide值有时候不准确,发生概率在50%左右.我看了下源码,觉得没什么问题,不知道哪里有问题.
2.集成后,我右侧主页面是一个tableviewcontroller,然后tableview的didSelect方法直接失效了
3.滑出侧滑栏后,能否直接禁止掉右侧主页面的事件
麻烦看下这两个问题,提供下思路,多谢

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.