UIRespond中的touchbegin函数的touch变量我们可以得到触摸的每个点的位置信息,但是我们不能很直接地判断出用户是在点击还是捏合或者滑动,
UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中的手势。UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,包含下面一些子类用于处理具体的手势:
第一、点击手势UITapGestureRecognizer (任意次数的拍击)
UITapGestureRecognizer *tap=[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)] autorelease];
[self.view addGestureRecognizer:tap];
UITapGestureRecognizer *doubleTap=[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)] autorelease];
doubleTap.numberOfTapsRequired=2;
[self.view addGestureRecognizer:doubleTap];
//区别单击、双击手势
[tap requireGestureRecognizerToFail:doubleTap];
第二、捏合手势UIPinchGestureRecognizer
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[self.view addGestureRecognizer:pinch];
第三、清扫UIPanGestureRecognizer
UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.view addGestureRecognizer:panGesture];
第四、滑动UISwipeGestureRecognizer (以任意方向)
UISwipeGestureRecognizer *swipeGesture=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
swipeGesture.direction=UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeGesture];
第五、旋转UIRotationGestureRecognizer (手指朝相反方向移动)
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[self.view addGestureRecognizer:rotation];
第六、长按UILongPressGestureRecognizer
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
longPress.minimumPressDuration = 2;
[self.view addGestureRecognizer:longPress];
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
if (longPress.state == UIGestureRecognizerStateEnded) {
return;
}
NSLog(@"长按");
}