#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
//根據(jù)偏移量計算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];
}