首页 > 代码库 > 侧滑菜单封装V1.0

侧滑菜单封装V1.0

// // XRSliderViewController.h

 

/**

 ****************************************************

 * project: XRSliderViewController

 * author : by xu ran

 * date   : 2014年12月21日16:39:16

 ****************************************************/

 

#import <UIKit/UIKit.h>

 

@interface XRSliderViewController : UIViewController

 

// 初始化方法

- (instancetype)initWithLeftViewController:(UIViewController *)leftVc MainViewController:(UIViewController *)mainVc;

 

@end

 

// XRSliderViewController.m

 

#import "XRSliderViewController.h"

 

#define OPEN_CLOSEMENU_NOTIFICATIONNAME @"clickCenterMenuNotifi"

#define BANMOVE_NOTIFICATIONNAME        @"noMove"

#define ALLOWMOVE_NOTIFICATIONNAME      @"canMove"

#define BACKMOVE_NOTIFICATIONNAME       @"back"

 

#define MainScreenSize [UIScreen mainScreen].bounds.size

 

// 侧滑的菜单宽度

#define MenuWidth [UIScreen mainScreen].bounds.size.width * 3.0f / 4.0f

 

@interface XRSliderViewController ()

{

    UIViewController * _leftVc;

    UIViewController * _mainVc;

    

    UIPanGestureRecognizer * _dragPan; // 拖动手势

    

    BOOL _isCanMove; // 是否可以拖动

}

 

@end

 

@implementation XRSliderViewController

 

/**

 *  移除观察者

 */

- (void)dealloc

{

    // 移除观察者

    [[NSNotificationCenter defaultCenter] removeObserver:self name:OPEN_CLOSEMENU_NOTIFICATIONNAME object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:BANMOVE_NOTIFICATIONNAME object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:ALLOWMOVE_NOTIFICATIONNAME object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:BACKMOVE_NOTIFICATIONNAME object:nil];

}

 

- (instancetype)initWithLeftViewController:(UIViewController *)leftVc MainViewController:(UIViewController *)mainVc

{

    if ([super init]) {

        

        // 注册观察者

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuClick) name:OPEN_CLOSEMENU_NOTIFICATIONNAME object:nil];

        

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(banMove) name:BANMOVE_NOTIFICATIONNAME object:nil];

        

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(allowMove) name:ALLOWMOVE_NOTIFICATIONNAME object:nil];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(back) name:BACKMOVE_NOTIFICATIONNAME object:nil];

        

        _leftVc = leftVc;

        _mainVc = mainVc;

        _isCanMove = YES;

    }

    

    return self;

}

 

// 创建侧滑菜单

- (void)createMenu

{

    _leftVc.view.frame = CGRectMake(0, 20, MenuWidth, MainScreenSize.height - 20);

    [self.view addSubview:_leftVc.view];

    

    _mainVc.view.frame = CGRectMake(0, 20, MainScreenSize.width, MainScreenSize.height - 20);

    [self.view addSubview:_mainVc.view];

    

    // 添加拖动手势

    _dragPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragCenterView:)];

    

    [_mainVc.view addGestureRecognizer:_dragPan];

    

    // 开始让leftVc的view隐藏

    [_leftVc.view setHidden:YES];

}

 

/**

 *  隐藏侧边栏

 */

- (void)back

{

    [UIView animateWithDuration:0.3 animations:^{

        _dragPan.view.transform = CGAffineTransformMakeTranslation(0, 0);

    } completion:^(BOOL finished) {

        _leftVc.view.hidden = YES;

    }];

}

 

/**

 *  禁止拖动

 */

- (void)banMove

{

    _isCanMove = NO;

}

 

/**

 *  允许拖动

 */

- (void)allowMove

{

    _isCanMove = YES;

}

 

/**

 *  监控view的拖拽手势

 *  用x来判断 用transform来控制

 */

- (void)dragCenterView:(UIPanGestureRecognizer *)pan

{

    if (_isCanMove) {

        _leftVc.view.hidden = NO;

        // 取出手势的点

        CGPoint point = [pan translationInView:pan.view];

        

        // 结束拖拽

        if (pan.state == UIGestureRecognizerStateCancelled || pan.state == UIGestureRecognizerStateEnded) {

            if (pan.view.frame.origin.x >= MenuWidth * 0.5) {

                // 至少向右走了MenuWidth * 0.5

                [UIView animateWithDuration:0.3 animations:^{

                    // 改变view的Transform

                    pan.view.transform = CGAffineTransformMakeTranslation(MenuWidth, 0);

                }];

            }else {

                // 走动的距离没有达到MenuWidth * 0.5

                [UIView animateWithDuration:0.3 animations:^{

                    pan.view.transform = CGAffineTransformMakeTranslation(0, 0);

                } completion:^(BOOL finished) {

                    [_leftVc.view setHidden:YES];

                }];

            }

        }else {

            // 拖拽中

            pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, 0);

            // 再次清零

            [pan setTranslation:CGPointZero inView:pan.view];

            

            // 判断拖拽的距离

            if (pan.view.frame.origin.x >= MenuWidth) {

                pan.view.transform = CGAffineTransformMakeTranslation(MenuWidth, 0);

            }else if (pan.view.frame.origin.x <= 0) {

                pan.view.transform = CGAffineTransformMakeTranslation(0, 0);

            }

        }

 

    }

}

 

/**

 *  菜单点击

 */

- (void)menuClick

{

    _leftVc.view.hidden = NO;

    if (_dragPan.view.frame.origin.x <= 0) {

        [UIView animateWithDuration:0.3 animations:^{

            _dragPan.view.transform = CGAffineTransformMakeTranslation(MenuWidth, 0);

        }];

    }else {

        

        [UIView animateWithDuration:0.3 animations:^{

            _dragPan.view.transform = CGAffineTransformMakeTranslation(0, 0);

        } completion:^(BOOL finished) {

            _leftVc.view.hidden = YES;

        }];

    }

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    // 创建侧滑菜单

    [self createMenu];

}

 

@end

 

侧滑菜单封装V1.0