IOS 手势

IOS 手势

简介:

1.常见手势

手势说明:

  1. Tap(点一下)
  1. Pinch(二指往內或往外拨动,平时经常用到的缩放)
  2. Rotation(旋转)
  3. Swipe(滑动,快速移动)
  4. Pan (拖移,慢速移动)
  5. LongPress(长按)

使用手势的步骤

使用手势很简单,分为两步:

  1. 创建手势实例。当创建手势时,指定一个回调方法,当手势开始,改变、或结束时,回调方法被调用。
  2. 添加到需要识别的View中。每个手势只对应一个View,当屏幕触摸在View的边界内时,如果手势和预定的一样,那就会回调方法。

ps:一个手势只能对应一个View,但是一个View可以有多个手势。

UIImageView *snakeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snake.png"]]; 
snakeImageView.frame = CGRectMake(50, 50, 100, 160); 
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] 
initWithTarget:self 
action:@selector(handlePan:)]; 
[snakeImageView addGestureRecognizer:panGestureRecognizer]; 
  1. Tap(点一下)
  1. Pinch(二指往內或往外拨动,平时经常用到的缩放)
  2. Rotation(旋转)

这三种手势不用细讲比较简单看下demo即可

讲下以下几点:

  1. Pan拖动手势速度与拖动结束状态
  2. 同时触发两个view的手势
  3. 解决手势依耐性 也叫手势冲突
  4. 自定义手势

1.Pan拖动手势速度与拖动结束状态

状态

recognizer.state == UIGestureRecognizerStateEnded


>速度
>~~~
CGPoint velocity = [recognizer velocityInView:self.view]; 

2.同时触发两个view的手势

手势之间是互斥的,如果你想同时触发View1和View2的手势,比如说(1)view1 拖动 View2 拖动 或者 (2)view1 拖动 View 缩放 正常情况只有一个手势会响应。那么需要实现协议
UIGestureRecognizerDelegate。

1.添加代理

//<UIGestureRecognizerDelegate>

@interface ViewController : UIViewController<UIGestureRecognizerDelegate> 
   
@end 
    
    
  panGestureRecognizer.delegate = self;
  pinchGestureRecognizer.delegate = self;
  rotateRecognizer.delegate = self;

2.实现代理
添加代理的手势将会进入这个方法

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
{ 
   return YES; 
} 

Simultaneously//同时的

3.解决手势依耐性 也叫手势冲突

比较典型的例子:
pan 拖动手势和 tap 点击手势

pan拖动手势 通常会触发tap 手势,使得pan 和tap 手势事件同时发生,而使用时往往我们只希望响应生效的手势,可以用函数 requireGestureRecognizerToFail 来解决手势冲突问题

[tapRecognizer requireGestureRecognizerToFail:panGestureRecognizer];

意思就是,当如果pan手势失败,就是没发生拖动,才会出发tap手势。这样如果你有轻微的拖动,那就是pan手势发生了。tap的声音就不会发出来了

4.自定义手势

自定义手势继承:UIGestureRecognizer,实现下面的方法:

– touchesBegan:withEvent: 
– touchesMoved:withEvent: 
– touchesEnded:withEvent: 
- touchesCancelled:withEvent: 

例子:

.h文件
#import <UIKit/UIKit.h> 
typedef enum { 
DirectionUnknown = 0, 
DirectionLeft, 
DirectionRight 
} Direction; 

@interface HappyGestureRecognizer : UIGestureRecognizer 
@property (assign) int tickleCount; //挠痒次数
@property (assign) CGPoint curTickleStart; //获取开始点
@property (assign) Direction lastDirection;//最近一次方向 

@end 
.m文件

#import "HappyGestureRecognizer.h" 
#import <UIKit/UIGestureRecognizerSubclass.h> 
#define REQUIRED_TICKLES 2 
#define MOVE_AMT_PER_TICKLE 25 

@implementation HappyGestureRecognizer 

//手势开始时记录开始坐标点
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
UITouch * touch = [touches anyObject]; 
self.curTickleStart = [touch locationInView:self.view]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

// Make sure we've moved a minimum amount since curTickleStart 
UITouch * touch = [touches anyObject]; 
CGPoint ticklePoint = [touch locationInView:self.view]; //获取点击点坐标
CGFloat moveAmt = ticklePoint.x - self.curTickleStart.x; //现在点击坐标 与 初始坐标
Direction curDirection; 
if (moveAmt < 0) { 
curDirection = DirectionLeft; 
} else { 
curDirection = DirectionRight; 
} 
if (ABS(moveAmt) < MOVE_AMT_PER_TICKLE) return; 

// 确认方向改变了 
if (self.lastDirection == DirectionUnknown || 
(self.lastDirection == DirectionLeft && curDirection == DirectionRight) || 
(self.lastDirection == DirectionRight && curDirection == DirectionLeft)) { 

// 挠痒次数 
self.tickleCount++; 
self.curTickleStart = ticklePoint; 
self.lastDirection = curDirection; 

// 一旦挠痒次数超过指定数,设置手势为结束状态 
// 这样回调函数会被调用。 
if (self.state == UIGestureRecognizerStatePossible && self.tickleCount > REQUIRED_TICKLES) { 
[self setState:UIGestureRecognizerStateEnded]; 
} 
} 

} 


//重置相关数据
- (void)reset { 
self.tickleCount = 0; 
self.curTickleStart = CGPointZero; 
self.lastDirection = DirectionUnknown; 
if (self.state == UIGestureRecognizerStatePossible) { 
[self setState:UIGestureRecognizerStateFailed]; 
} 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
[self reset]; 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
[self reset]; 
} 

@end 

调用自定义手势和系统手势一样

- (void)handleHappy:(HappyGestureRecognizer *)recognizer{
[self.hehePlayer play];
}

参考文档

iOS:事件处理

iOS手势识别的详细使用

Demo地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容