UIGestureRecognizer 手势
添加、移除手势
[self.view removeGestureRecognizer:UIGestureRecognizer *];
[self.view addGestureRecognizzer:UIGestureRecognizer *];
一个手势只能对应一个View,但是一个View可以有多个手势
1.UITapGestureRecognizer 点按手势区分单击双击
创建手势并区分单双击
// 单击 UITapGestureRecognizer *singleTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGesAction:)]; [self.view addGestureRecognizer:singleTapGes]; // 添加到视图上 // 双击 UITapGestureRecognizer *doubleTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapGesAction:)]; [self.view addGestureRecognizer:doubelTapGes] // 单击以外的添加所需的点按次数 doubleTapGes.numberOfTapsRequired = 2; // 区分单双击 // 单击只有在双击不成立才执行,以此区分单双击 [tapGes requireGestureRecognizerToFail:doubleTapGes]; tapGes.delegate = self; [self.view addGestureRecognizer:doubelTapGes];
重要属性
// 设置点击次数 tapGesture.numberOfTapsRequired = 1; // 获取点击位置 - (void)singleTapGesAction:(UITapGestureRecognizer *)singleTapGes { // 从单击得到单击点的位置 CGPoint point = [singleTapGes locationInView:self.view]; NSLog(@"%@", NSStringFromCGPoint(point)); }
2.UIPanGestureRecognizer(平移)与3.UISwipeGestureRecognizer(轻扫) 平移与轻扫需区分
- 创建手势并区分平移与轻扫
- 平移
// 平移(即拖动) UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesAction:)]; panGes.minimumNumberOfTouches = 1;// 最少手指个数 panGes.maximumNumberOfTouches = 2;// 最多手指个数 [self.view addGestureRecognizer:panGes];
- 轻扫
// 轻扫 UISwipeGestureRecognizer *swipGes = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipGesAction:)]; // 轻扫需要添加方向 swipGes.direction = UISwipeGestureRecognizerDirectionRight; UISwipeGestureRecognizerDirectionRight, default UISwipeGestureRecognizerDirectionLeft, UISwipeGestureRecognizerDirectionUp, UISwipeGestureRecognizerDirectionDown // 区分平移和轻扫 // 轻扫只有在平移不执行才执行,以此区分平移和轻扫 [panGes requireGestureRecognizerToFail:swipGes]; [self.view addGestureRecognizer:swipGes];
- 平移
重要属性
轻扫
// 设置轻扫方向 swipGes.direction = UISwipeGestureRecognizerDirection; // 获取轻扫手势方向 - (void)swipGesAction:(UISwipeGestureRecognizer *)swipGes { NSLog(@"轻扫方向%lu", (unsigned long)swipGes.direction); }
平移
// 获取平移的坐标 // 移动事件函数,只要手指坐标在屏幕上发生变化时,函数就会被调用 - (void)panGesAction:(UIPanGestureRecognizer *)panGes { // 获取移动的坐标, CGPoint pt = [panGes translationInView:self.view]; // 获取移动时的相对速度 CGPoint pv = [panGes velocityInView:self.view]; NSLog(@"pv.x = %.2f, pv.y = %.2f", pv.x, pv.y); }
4.UILongPressGestureRecognizer(长按)
- 创建手势
// 长按 UILongPressGestureRecognizer *longPressGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesAction:)]; // 长按需要添加时间;Default is 0.5 longPressGes.minimumPressDuration = 2.0; // 长按时候,手指头可以移动距离;Default is 10 longPressGes.allowableMovement = 30; // 需要点击的次数;Default is 0 longPressGes.numberOfTapsRequired = 3; // 需要几根手指头;Default is 1 longPressGes.numberOfTouchesRequired = 3; [self.view addGestureRecognizer:longPressGes];
重要属性
// 设置重要属性 // 长按需要添加时间;Default is 0.5 longPressGes.minimumPressDuration = 2.0; // 长按时候,手指头可以移动距离;Default is 10 longPressGes.allowableMovement = 30; // 需要点击的次数;Default is 0 longPressGes.numberOfTapsRequired = 3; // 需要几根手指头;Default is 1 longPressGes.numberOfTouchesRequired = 3; // 长按触发事件 // 长按手势的事件会在你开始长按的时候走一次,然后还会在你松开的时候走一次,所以判断在开始的时候写入需求 - (void)longPressGesAction:(UILongPressGestureRecognizer *)longPressGes { // 长按状态开始 if(longPressGes.state == UIGestureRecognizerStateBegan) { NSLog(@"长按状态开始"); } } else if(longPressGes.state == UIGestureRecognizerStateChanged) { NSLog(@"长按状态改变"); } else { NSLog(@"长按状态结束"); }
5.UIPinchGestureRecognizer(捏合)
创建手势
UIPinchGestureRecognizer *pinchGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesAction:)]; [self.redView addGestureRecognizer:pinchGes];
重要属性
- (void)pinchGesAction:(UIPinchGestureRecognizer *)pinchGes { // 缩放比例 CGFloat scale = pinchGes.scale; // 缩放速度 CGFloat velocity = pinchGes.velocity; // 获取捏合手势的视图进行视图操作 UIView *view = pinchGes.view; view.transform = CGAffineTransformScale(view.transform, scale, scale); // 第一个参数视图的transform,第二个参数X轴缩放比例,第三个参数Y轴缩放比例 //每次捏合动作完毕之后,让此捏合值复原,使得它每次都是从100%开始缩放 pinchGes.scale = 1; }
5.UIRotationGestureRecognizer(旋转)
- 创建手势
UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesAction:)]; [self.redView addGestureRecognizer:rotationGestureRecognizer];
重要属性
- (void)rotationGesAction:(UIRotationGestureRecognizer *)rotationGes { // 旋转角度 CGFloat rotation = rotationGes.rotation; // 弧度/秒的缩放速度 CGFloat velocity = rotationGes.velocity; UIView *view = rotationGes.view; // 视图处理 view.transform = CGAffineTransformRotate(view.transform, rotation); // 复原 rotationGes.rotation = 0; }
UIScreenEdgePanGestureRecognizer(边缘轻扫)
创建手势
UIScreenEdgePanGestureRecognizer *screenEdgePanGes = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGesAction:)]; screenEdgePanGes.edges = UIRectEdgeRight; // 滑动起点为右边缘 typedef NS_OPTIONS(NSUInteger, UIRectEdge) { UIRectEdgeNone = 0, UIRectEdgeTop = 1 << 0, UIRectEdgeLeft = 1 << 1, UIRectEdgeBottom = 1 << 2, UIRectEdgeRight = 1 << 3, UIRectEdgeAll = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight } [self.view addGestureRecognizer:screenEdgePanGes];
- 触发方法
- (void)screenEdgePanGesAction:(UIScreenEdgePanGestureRecognizer *)screenEdgePanGes { // 手势的起始边缘 UIRectEdge edge = screenEdgePanGes.edges; }
常用手势代理
- 用于是否关闭手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) { //判断如果点击的是tableView的cell,就把手势给关闭了 return NO;//关闭手势 }//否则手势存在 return YES; }
用于多个手势可以同时响应
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ // 返回值为YES的时候,当执行一个手势的操作的时候,也可以执行其他手势的操作 return YES; }
当手势和UITabelView共存可能产生冲突
解决方法-->利用手势的代理
// 1.添加手势,手势的代理 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]; tap.delegate = self; [self.view addGestureRecognizer:tap]; // 2.执行代理 #pragma mark - UIGestureRecognizerDelegate -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) { // 判断如果点击的是tableView的cell,就把手势给关闭了 return NO; // 关闭手势 } return YES; // 手势存在 }
冲突总结(后期补充)
冲突:tableview
- 不冲突:button,textField
手势不执行方法的可能原因
- 没有添加到视图上
- 视图没有打开视图交互
- 缺少设置手势需要的条件
- 多种手势没有区分