标签:
#import "DragerViewController.h"
@interface DragerViewController ()
@property (nonatomic, weak) UIView *leftV;
@property (nonatomic, weak) UIView *rightV;
@property (nonatomic, weak) UIView *mainV;
@end
@implementation DragerViewController
- (void)viewDidLoad {
[super viewDidLoad];
//添加子控件
[self setUp];
//添加手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.mainV addGestureRecognizer:pan];
}
- (void)pan:(UIPanGestureRecognizer *)pan{
//获取偏移量
CGPoint transP = [pan translationInView:self.mainV];
//为什么不使用transform,是因为我们还要去修改高度,使用transform,只能修改,x,y
//self.mainV.transform = CGAffineTransformTranslate(self.mainV.transform, transP.x, 0);
self.mainV.frame = [self frameWithOffsetX:transP.x];
//判断拖动的方向
if(self.mainV.frame.origin.x > 0){
//向右
self.rightV.hidden = YES;
}else if(self.mainV.frame.origin.x < 0){
//向左
self.rightV.hidden = NO;
}
//复位
[pan setTranslation:CGPointZero inView:self.mainV];
}
//根据偏移量计算MainV的frame
- (CGRect)frameWithOffsetX:(CGFloat)offsetX {
CGRect frame = self.mainV.frame;
frame.origin.x += offsetX;
return frame;
}
- (void)setUp{
//leftV
UIView *leftV = [[UIView alloc] initWithFrame:self.view.bounds];
leftV.backgroundColor = [UIColor blueColor];
self.leftV = leftV;
[self.view addSubview:leftV];
//rightV
UIView *rightV = [[UIView alloc] initWithFrame:self.view.bounds];
rightV.backgroundColor = [UIColor greenColor];
self.rightV = rightV;
[self.view addSubview:rightV];
//mianV
UIView *mainV = [[UIView alloc] initWithFrame:self.view.bounds];
mainV.backgroundColor = [UIColor redColor];
self.mainV = mainV;
[self.view addSubview:mainV];
}
@end
标签:
原文地址:http://www.cnblogs.com/liuzhenjie/p/5455703.html