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

时间:2022-08-21 07:14:30

代码托管到了Github https://github.com/cyuanyang/YYSlideView

主演实现代码:

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 = -;
CGPoint point = [pan locationInView:self.view];
if(pan.state == UIGestureRecognizerStateBegan) {
beginX = point.x;
}else if(pan.state == UIGestureRecognizerStateEnded){
beginX = -;
if (_mainView.frame.origin.x < AUTO_X) {
[self autoAnim:NO];
}else{
[self autoAnim:YES];
} if (_mainView.frame.origin.x == ) {
_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 = ;
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 <=){
mainRect.origin.x = ;
} _mainView.frame = mainRect; CGRect slideRect = _slideView.frame;
slideRect.origin.x = detlaX/ - _slideView.slideWidth/;
if (slideRect.origin.x >=) {
slideRect.origin.x = ;
}else if (slideRect.origin.x <= -_slideView.slideWidth/){
slideRect.origin.x = -_slideView.slideWidth/;
}
_slideView.frame = slideRect;
}
}

3.自动滚动的方法:

/*
* open or close
*/
-(void)autoAnim:(BOOL)isOpen{
[UIView animateWithDuration:ANIM_DUR delay: options:UIViewAnimationOptionCurveLinear animations:^{
CGRect mainRect = _mainView.frame;
CGRect slideRect = _slideView.frame;
if (isOpen) {
mainRect.origin.x = _slideView.slideWidth;
slideRect.origin.x = ;
_state = SlideViewStateOpening;
}else{
mainRect.origin.x = ;
slideRect.origin.x = -_slideView.slideWidth/;
_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/, , width, H);
self = [super initWithFrame:rect]; if (self) {
_slideWidth = width;
[self initView];
} return self;
} ///子类重写回调
-(void)initView{ }

OK!!!