/**
* 手势识别器:
1.能够为系统提供的视图对象添加触摸事件响应方法(UIView UILabel UIImageView)
2.内部封装了手势识别的过程,只需要把重心放到手势之后对应的操作上.
*/
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor purpleColor];
UIView * redView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
[redView release];
UIButton * btn =[UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(20, 20, 50, 50);
[btn
setTitle:@"bakc" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor = [UIColor yellowColor];
[redView addSubview:btn];
//UIGestureRecognizer 是手势识别器的一个抽象的基类,只提供手势识别器的基本功能,使用时使用具体子类
//轻拍受手势
// numberOfTapsRequired property 点击次数
// numberOfTouchesRequired property 手指个数
// UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
// tapGesture.numberOfTapsRequired = 1; //设置轻拍的次数
// //设置轻拍时手指的个数
// tapGesture.numberOfTouchesRequired = 2;
// [redView addGestureRecognizer:tapGesture];// 给视图添加手势
// [tapGesture release];
// UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongpressGesture:)];
// //设置长按手势触发的时间
// longPressGesture.minimumPressDuration = 1;
// [redView addGestureRecognizer:longPressGesture];
// [longPressGesture release];
//
//轻扫手势
// UISwipeGestureRecognizer * swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGesture:)];
/**0
轻扫方向
UISwipeGestureRecognizerDirectionRight
UISwipeGestureRecognizerDirectionLeft
UISwipeGestureRecognizerDirectionUp
UISwipeGestureRecognizerDirectionDown
*/
//设置轻扫方向,让左右方向都存在 只要枚举值中又左移运算符,代表枚举值可以通过按位或 | 让多个枚举值存在
//warning :::::上下 和左右方向 在一个手势识别器中不能同时存在
//如果想要上下左右四个方法都有,就要创建两个轻扫手势对象,一个支持上下,一个支持左右.
// swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight ;
// [redView addGestureRecognizer:swipeGesture];
// [swipeGesture release];
//平移手势
// UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
// [redView addGestureRecognizer:panGesture];
// [panGesture release];
//捏合手势
UIPinchGestureRecognizer * pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGesture:)];
[redView addGestureRecognizer:pinchGesture];
[pinchGesture release];
//旋转
// UIRotationGestureRecognizer * rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
// [redView addGestureRecognizer:rotationGesture];
// [rotationGesture release];
// //屏幕边缘手势
// UIScreenEdgePanGestureRecognizer * screenEdge = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenEdge:)];
//
//
// /** 设置屏幕边缘
// UIRectEdgeNone
// UIRectEdgeTop
// UIRectEdgeLeft
// UIRectEdgeBottom
// UIRectEdgeRight
// UIRectEdgeAll
// */
// screenEdge.edges = UIRectEdgeLeft;
// [redView addGestureRecognizer:screenEdge];
// [screenEdge release];
// [self setupSubviews];
}
- (void)back:(UIButton *)btn
{
btn.superview.frame = [[UIScreen mainScreen]bounds];
}
-(void)screenEdge :(UIScreenEdgePanGestureRecognizer *)screenEdge
{
CGPoint point = [screenEdge translationInView:screenEdge.view];
screenEdge.view.transform = CGAffineTransformTranslate(screenEdge.view.transform, point.x, 0);
[screenEdge setTranslation:CGPointZero inView:screenEdge.view];
}
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
rotation.rotation = 0;
}
- (void)pinchGesture:(UIPinchGestureRecognizer *)pinchGesture
{
NSLog(@"ss");
pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale);
//将之前变化的比例置为 1 . .
pinchGesture.scale = 1.0;
[pinchGesture setScale:1.0];
}
- (void)setupSubviews
{
CGFloat y = 20;
for (int i = 0 ; i < 10; i ++) {
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(20, y,280 , 50)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
[view release];
y +=55;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(aa:)];
[view addGestureRecognizer:tap];
}
}
- (void)aa:(UIGestureRecognizer *)aa
{
}
- (void)panGesture:(UIPanGestureRecognizer *)panGesture
{
CGPoint point = [panGesture translationInView:panGesture.view];
// panGesture.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);
panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y);
//将之前增量清零
[panGesture setTranslation:point inView:panGesture.view];
// //获取的是手指位置的改变量 , 存储在x轴以及y轴的改变量
// CGPoint point = [panGesture translationInView:panGesture.view];
// panGesture.view.center = CGPointMake(panGesture.view.center.x+ point.x,panGesture.view.center.y+ point.y);
// //将之前的增量清零 重置之前的增量
// [panGesture setTranslation:CGPointZero inView:panGesture.view];
// panGesture.view.backgroundColor = [UIColor random];
}
- (void)swipeGesture :(UISwipeGestureRecognizer *)swipeGesture
{
NSLog(@"%u", swipeGesture.direction);
swipeGesture.view.backgroundColor = [UIColor random];
}
- (void)handleLongpressGesture :(UILongPressGestureRecognizer *)longPressGesture
{
//当识别到长按手势时触发(长按时间到达之后触发)
if (UIGestureRecognizerStateBegan ==longPressGesture.state) {
longPressGesture.view.backgroundColor = [UIColor random];
}
}
//处理轻拍事件
- (void)handleTapGesture :(UITapGestureRecognizer *)tapGesture
{
tapGesture.view.backgroundColor = [UIColor random];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
转载于:https://blog.51cto.com/qccccc/1548910