-(instancetype)initWithLeftCtrl:(UIViewController *)leftCtrl withMainCtrl:(UIViewController *)maiCtrl withRightCtrl:(UIViewController *)rightCtrl;
在。m文件中实现这个方法实现方式如下:
-(instancetype)initWithLeftCtrl:(UIViewController *)leftCtrl withMainCtrl:(UIViewController *)maiCtrl withRightCtrl:(UIViewController *)rightCtrl
{
self=[super initWithNibName:nil bundle:nil];
if(self){
_leftCtrl=leftCtrl;
_mainCtrl=maiCtrl;
_rightCtrl=rightCtrl;
_isMain=YES;
_panRecognizer=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handpan:)];
_panRecognizer.maximumNumberOfTouches=1;
_tapRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handTap:)];
[_mainCtrl.view addGestureRecognizer:_tapRecognizer];
[_mainCtrl.view addGestureRecognizer:_panRecognizer];
_leftCtrl.view.frame=CGRectMake(-WIDTH, 0, WIDTH, HEIGHT);
_rightCtrl.view.frame=CGRectMake(self.view.bounds.size.width+WIDTH, 0, WIDTH, HEIGHT);
[self.view addSubview:_leftCtrl.view];
[self.view addSubview:_mainCtrl.view];
[self.view addSubview:_rightCtrl.view];
}
return self;
}
第三步:实现拖拽手势和单点手势的方法:-(void)handpan:(UIPanGestureRecognizer *)pan
{
CGPoint point=[pan translationInView:_mainCtrl.view];
if(_isMain){
//point.x>0表示右滑,显示左视图
if(point.x>0){
[self showLeftCtrl];
}else{
[self showRightCtrl];
}
}
}
-(void)handTap:(UITapGestureRecognizer *)tap
{
//单点主视图的view显示主视图
[self showMainCtrl];
}
showMainCtrl与showLeftCtrl与showRightCtrl方法实现如下:-(void)showMainCtrl
{
_isMain=YES;
[UIView animateWithDuration:0.35 animations:^{
_leftCtrl.view.frame=CGRectMake(-WIDTH, 0,WIDTH, HEIGHT);
_mainCtrl.view.frame=CGRectMake(0, 0, self.view.bounds.size.width, HEIGHT);
_rightCtrl.view.frame=CGRectMake(self.view.bounds.size.width+WIDTH, 0, WIDTH, HEIGHT);
}];
}
-(void)showLeftCtrl
{
_isMain=NO;
[UIView animateWithDuration:0.35 animations:^{
_leftCtrl.view.frame=CGRectMake(0, 0,WIDTH, HEIGHT);
_mainCtrl.view.frame=CGRectMake(WIDTH, 0, self.view.bounds.size.width, HEIGHT);
_rightCtrl.view.frame=CGRectMake(self.view.bounds.size.width+WIDTH+WIDTH, 0, WIDTH, HEIGHT);
}];
}
-(void)showRightCtrl
{
_isMain=NO;
[UIView animateWithDuration:0.35 animations:^{
_rightCtrl.view.frame=CGRectMake(self.view.bounds.size.width-WIDTH, 0, WIDTH, HEIGHT);
_leftCtrl.view.frame=CGRectMake(-WIDTH-WIDTH, 0,WIDTH, HEIGHT);
_mainCtrl.view.frame=CGRectMake(-WIDTH, 0, self.view.bounds.size.width, HEIGHT);
}];
}
这里的_isMain表示是否显示的不是主视图,便于过滤不需要的效果