类qq侧滑效果实现
2016-03-01 本文已影响162人
Arxu
ViewController.m
在self.view上创建一个透明的View
UIView *mainView=[[UIView alloc]initWithFrame:CGRectMake(-self.view.bounds.size.width*0.25,0,self.view.bounds.size.width,self.view.bounds.size.height)];
mainView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:mainView];
添加tabbarController的view
- (void)addTabbarController
{
_barVC = [[TabbarController alloc]init];
[self addChildViewController:_barVC];
[self.view addSubview:_barVC.view];
_barVC.view.frame = self.view.bounds;
}
添加手势
-(void)addRecognizer{
// 添加拖拽
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(didPanEvent:)];
[self.view addGestureRecognizer:pan];
}
实现拖拽
-(void)didPanEvent:(UIPanGestureRecognizer *)recognizer{
// 1. 获取手指拖拽的时候, 平移的值
CGPoint translation = [recognizer translationInView:_barVC.view];
// 2. 让当前控件做响应的平移
_barVC.view.transform = CGAffineTransformTranslate(_barVC.view.transform, translation.x, 0);
[self.view.subviews objectAtIndex:1].ttx=[self.view.subviews objectAtIndex:2].ttx/3;
// 3. 每次平移手势识别完毕后, 让平移的值不要累加
[recognizer setTranslation:CGPointZero inView:_barVC.view];
//获取最右边范围
CGAffineTransform rightScopeTransform=CGAffineTransformTranslate(self.view.transform,[UIScreen mainScreen].bounds.size.width*0.75, 0);
// 当移动到右边极限时
if (_barVC.view.transform.tx>rightScopeTransform.tx) {
// 限制最右边的范围
_barVC.view.transform=rightScopeTransform;
// 限制透明view最右边的范围
[self.view.subviews objectAtIndex:1].ttx=[self.view.subviews objectAtIndex:2].ttx/3;
// 当移动到左边极限时
}else if (_barVC.view.transform.tx<0.0){
// 限制最左边的范围
_barVC.view.transform=CGAffineTransformTranslate(self.view.transform,0, 0);
// 限制透明view最左边的范围
[self.view.subviews objectAtIndex:1].ttx=[self.view.subviews objectAtIndex:2].ttx/3;
}
// 当托拽手势结束时执行
if (recognizer.state == UIGestureRecognizerStateEnded)
{
[UIView animateWithDuration:0.2 animations:^{
if (_barVC.view.x >[UIScreen mainScreen].bounds.size.width*0.5) {
_barVC.view.transform=rightScopeTransform;
[self.view.subviews objectAtIndex:1].ttx=_barVC.view.ttx/3;
}else{
_barVC.view.transform = CGAffineTransformIdentity;
[self.view.subviews objectAtIndex:1].ttx=_barVC.view.ttx/3;
}
}];
}
}