#import
@interfaceDragViewController :UIViewController
@property (nonatomic, strong, readonly) UIView *mainV;
@property (nonatomic, strong, readonly) UIView *leftV;
@property (nonatomic, strong, readonly) UIView *rightV;
@end
#import "DragViewController.h"
#define screenW [UIScreen mainScreen].bounds.size.width
@interface DragViewController ()<UIGestureRecognizerDelegate>
@end
@implementationDragViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[selfsetUp];
//添加手势
UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
pan.delegate=self;
[self.mainV addGestureRecognizer:pan];
//控制器的view添加点按手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self.view addGestureRecognizer:tap];
}
-(void)tap:(UITapGestureRecognizer *)tap{
//让mainV复位
[UIView animateWithDuration:0.5 animations:^{
self.mainV.frame=self.view.bounds;
}];
}
#define targetR screenW *0.5
#define targetL - (screenW *0.5)
-(void)pan:(UIPanGestureRecognizer *)pan{
//获取偏移量
CGPointtransP = [pantranslationInView:self.mainV];
//为什么不使用transform,是因为我们还要修改高度,这里只能修改x,y
//self.mainV.transform = CGAffineTransformTranslate(self.mainV.transform, transP.x, 0);
self.mainV.frame= [selfframeWithOffsetX:transP.x];
//判断拖动方向
if(self.mainV.frame.origin.x>0) {
//向右
self.rightV.hidden=YES;
}elseif(self.mainV.frame.origin.x<0){
//向左
self.rightV.hidden=NO;
}
//当手指松开时自动定位
CGFloattarget =0;
if (pan.state == UIGestureRecognizerStateEnded) {
if(self.mainV.frame.origin.x>screenW*0.5) {
//判断在右侧
//当前View的x没有大于屏幕宽度的一半,大于就是在右侧
target =targetR;
}elseif(CGRectGetMaxX(self.mainV.frame)
target =targetL;
}
//计算当前mainV的frame
CGFloatoffset = target -self.mainV.frame.origin.x;
[UIView animateWithDuration:0.5 animations:^{
self.mainV.frame= [selfframeWithOffsetX:offset];
}];
}
//复位
[pansetTranslation:CGPointZero inView:self.mainV];
}
#define maxY100
//根据偏移量计算mainV的frame
-(CGRect)frameWithOffsetX:(CGFloat)offsetX{
CGRectframe =self.mainV.frame;
frame.origin.x+= offsetX;
CGFloaty =fabs(frame.origin.x*maxY/screenW);
frame.origin.y= y;
//屏幕的高度减去两倍的y值
frame.size.height = [UIScreen mainScreen].bounds.size.height - (2 * frame.origin.y);
returnframe;
}
-(void)setUp{
UIView*leftV = [[UIViewalloc]initWithFrame:self.view.bounds];
leftV.backgroundColor = [UIColor blueColor];
//self.leftV = leftV;
_leftV= leftV;
[self.viewaddSubview:leftV];
UIView*rightV = [[UIViewalloc]initWithFrame:self.view.bounds];
rightV.backgroundColor = [UIColor greenColor];
//self.rightV= rightV;
_rightV= rightV;
[self.viewaddSubview:rightV];
UIView*mainV = [[UIViewalloc]initWithFrame:self.view.bounds];
mainV.backgroundColor = [UIColor redColor];
//self.mainV = mainV;
_mainV= mainV;
[self.viewaddSubview:mainV];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
注意://当一个控制器的view添加到另一个控制器的view的时候,此时view所在的控制器也应该成为上一个控制器的子控制器
应用:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//当一个控制器的view添加到另一个控制器的view的时候,此时view所在的控制器也应该成为上一个控制器的子控制器
TableViewController *vc1 = [[TableViewController alloc] init];
vc1.view.frame= self.mainV.bounds;
[self.mainVaddSubview:vc1.view];
[self addChildViewController:vc1];
TableViewController *vc2 = [[TableViewController alloc] init];
vc2.view.backgroundColor = [UIColor redColor];
vc2.view.frame= self.mainV.bounds;
[self.leftVaddSubview:vc2.view];
[self addChildViewController:vc2];
}
网友评论