码迷,mamicode.com
首页 > 其他好文 > 详细

类似qq的左滑菜单栏简单实现

时间:2016-05-11 17:57:05      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

 

主演实现代码:

1.滑动的viewController的初始化主要view

-(instancetype)initWithSlideView:(YYBaseSlideView * )view{
    self = [super init];
    
    if (self) {
        self.slideView = view;
    }
    
    return self;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    
    _state = SlideViewStateClosed;
    [self.view addSubview:_slideView];
    
    self.mainView = [[UIView alloc]initWithFrame:self.view.frame];
    
    _mainView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:_mainView];
    
    [self.mainView addSubview:[self loadMainView]];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    
    [self.view addGestureRecognizer:pan];
    
    if (Is_Show_Cover) {
        self.coverView = [[UIView alloc]initWithFrame:self.view.frame];
        _coverView.backgroundColor = [UIColor blackColor];
        _coverView.alpha = 0.0;
        [self.mainView addSubview:_coverView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
        [_coverView addGestureRecognizer:tap];
        
        [_mainView insertSubview:[self loadMainView] belowSubview:_coverView];
    }else{
        [_mainView addSubview:[self loadMainView]];
    }
   
}

2.监听手势的处理方法

-(void)panAction:(UIPanGestureRecognizer *)pan{
    static CGFloat beginX = -1;
    CGPoint point = [pan locationInView:self.view];
    if(pan.state == UIGestureRecognizerStateBegan) {
        beginX = point.x;
    }else if(pan.state == UIGestureRecognizerStateEnded){
        beginX = -1;
        if (_mainView.frame.origin.x < AUTO_X) {
            [self autoAnim:NO];
        }else{
            [self autoAnim:YES];
        }
        
        if (_mainView.frame.origin.x == 0) {
             _state = SlideViewStateClosed;
             [self showOrHideCover:NO];
        }else if(_mainView.frame.origin.x == _slideView.slideWidth){
            _state = SlideViewStateOpened;
             [self showOrHideCover:YES];
        }
    }else if (pan.state == UIGestureRecognizerStateChanged){
        CGFloat detlaX = 0;
        if (_state == SlideViewStateClosed) {
            
            detlaX = point.x - beginX;
            
        }else if(_state == SlideViewStateOpened){
            detlaX =_slideView.slideWidth + point.x - beginX;
        }
        
        CGRect mainRect = _mainView.frame;
        mainRect.origin.x = detlaX;
        if (mainRect.origin.x >=_slideView.slideWidth) {
            mainRect.origin.x = _slideView.slideWidth;
        }else if (mainRect.origin.x <=0){
            mainRect.origin.x  = 0;
        }
        
        _mainView.frame = mainRect;
        
        CGRect slideRect = _slideView.frame;
        slideRect.origin.x = detlaX/2  - _slideView.slideWidth/2;
        if (slideRect.origin.x >=0) {
            slideRect.origin.x = 0;
        }else if (slideRect.origin.x <= -_slideView.slideWidth/2){
            slideRect.origin.x  = -_slideView.slideWidth/2;
        }
        _slideView.frame = slideRect;
    }
}

3.自动滚动的方法:

/*
 * open or close
 */
-(void)autoAnim:(BOOL)isOpen{
    [UIView animateWithDuration:ANIM_DUR delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        CGRect mainRect = _mainView.frame;
        CGRect slideRect = _slideView.frame;
        if (isOpen) {
            mainRect.origin.x = _slideView.slideWidth;
            slideRect.origin.x = 0;
            _state = SlideViewStateOpening;
        }else{
            mainRect.origin.x = 0;
            slideRect.origin.x = -_slideView.slideWidth/2;
            _state = SlideViewStateClosing;
        }
        _mainView.frame = mainRect;
        _slideView.frame = slideRect;
    } completion:^(BOOL finished) {
      
        if (isOpen) {
            _state = SlideViewStateOpened;
        }else{
            _state = SlideViewStateClosed;
        }
    }];
}

4.自定义的左边的view

-(instancetype)initWithWidth:(CGFloat)width{
    CGRect rect = CGRectMake(-width/2, 0, width, H);
    self = [super initWithFrame:rect];
    
    if (self) {
        _slideWidth = width;
        [self initView];
    }
    
    return  self;
}

///子类重写回调
-(void)initView{
    
}

OK!!!

类似qq的左滑菜单栏简单实现

标签:

原文地址:http://www.cnblogs.com/pigface/p/5482560.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!