之前讲到可以监听view上的触摸事件,实现触摸方法来处理事件。但这种作坊必须:1、自定义view;2、实现view的touches方法,在方法背部实现具体的处理代码。
所以通过touches方法监听触摸事件,有很明显的缺点:
1、 必须自定义view
2、 由于是在view内部监听touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件。
3、 不容易区分用具的具体手势。
因此,iOS3.2之后,苹果就退出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度。
介绍收拾是被器UIGestureRecognizer
为了完成手势识别,必须借助于手势识别器----UIGestureRecognizer
利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势:
UITapGestureRecognizer(敲击)
UIPinchGestureRecognizer(捏合,用于缩放)
UIPanGestureRecognizer(拖拽)
UISwipeGestureRecognizer(轻扫)
UIRotationGestureRecognizer(旋转)
UILongPressGestureRecognizer(长按)
UIGestureRecognizer的用法,因为每个手势的用法差不多,现在就以UITapGestureRecognizer为例讲解:
创建手势识别器对象
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
设置手势识别器对象的具体属性
// 连续敲击2次
tap.numberOfTapsRequired = 2;
// 需要2根手指一起敲击
tap.numberOfTouchesRequired = 2;
添加手势识别器到对应的view上
[self.myView addGestureRecognizer:tap];
监听手势的触发
[tap addTarget:self action:@selector(tapView:)];
手势识别的状态
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
// 没有触摸事件发生,所有手势识别的默认状态
UIGestureRecognizerStatePossible,
// 一个手势已经开始但尚未改变或者完成时
UIGestureRecognizerStateBegan,
// 手势状态改变
UIGestureRecognizerStateChanged,
// 手势完成
UIGestureRecognizerStateEnded,
// 手势取消,恢复至Possible状态
UIGestureRecognizerStateCancelled,
// 手势失败,恢复至Possible状态
UIGestureRecognizerStateFailed,
// 识别到手势识别
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
};
手势识别状态变化示意图:
下面用代码实现各个手势识别功能:
UITapGestureRecognizer(敲击)
- (void)testTap {
// 1.创建手势识别器(UITapGestureRecognizer(敲击))
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
// 2.设置手势属性(同时使用两根手指敲击两次才能触发事件)
// 2.1 手指个数-2(默认为1)
tap.numberOfTouchesRequired = 2;
// 2.2 敲击次数-2(默认为1)
tap.numberOfTapsRequired = 2;
// 3.监听手势
[tap addTarget:self action:@selector(tapYellowView)];
// 4.将手势添加到view上
[self.yellowView addGestureRecognizer:tap];
}
- (void)tapYellowView {
NSLog(@"tapYellowView----");
}
UILongPressGestureRecognizer(长按)
- (void)testLongPress {
// 1.创建手势识别器(UILongPressGestureRecognizer(长按))
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init];
// 2.设置手势属性
// 长按时间
longPress.minimumPressDuration = 1.0;
// 事件触发之前,长按时允许移动的距离(单位:px)(超出该距离事件无法监听)
// 事件触发之后,无论移动到什么地方(只要在view上)都一直会触发事件
longPress.allowableMovement = 20.0;
// 3.监听手势
[longPress addTarget:self action:@selector(longPressYellowView)];
// 4.将手势添加到view上
[self.yellowView addGestureRecognizer:longPress];
}
- (void)longPressYellowView {
NSLog(@"longPressYellowView----");
}
UIPanGestureRecognizer(拖拽)
- (void)testPan {
// 1.创建手势识别器(UIPanGestureRecognizer(拖拽))
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] init];
// 2.设置手势属性(默认为1)
pan.minimumNumberOfTouches = 1;
// 3.监听手势(传入手势)
[pan addTarget:self action:@selector(panYellowView:)];
// 4.将手势添加到view上
[self.yellowView addGestureRecognizer:pan];
}
- (void)panYellowView:(UIPanGestureRecognizer *)pan {
// 手指在view上拖拽的位置
CGPoint point = [pan translationInView:pan.view];
self.yellowView.center = CGPointMake(self.yellowView.center.x + point.x, self.yellowView.center.y + point.y);
// 移除之前的移动的距离
[pan setTranslation:CGPointZero inView:pan.view];
// 手指在view上移动的速度px/second
CGPoint vePoint = [pan velocityInView:pan.view];
NSLog(@"velocity------ %@", NSStringFromCGPoint(vePoint));
// 手指所处状态
switch (pan.state) {
case UIGestureRecognizerStateBegan: // 开始触发手势
NSLog(@"UIGestureRecognizerStateBegan");
break;
case UIGestureRecognizerStateChanged: // 手势改变
NSLog(@"UIGestureRecognizerStateChanged");
break;
case UIGestureRecognizerStateEnded: // 手势结束
NSLog(@"UIGestureRecognizerStateEnded");
break;
default:
break;
}
}
UIPinchGestureRecognizer(捏合,用于缩放)
- (void)testPinch {
// 1.创建手势识别器(UIPinchGestureRecognizer(捏合,用于缩放))
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] init];
// 2.设置手势属性
// pinch.scale = 0.99;// 缩放比例
// 3.监听手势(传入手势)
[pinch addTarget:self action:@selector(pinchYellowView:)];
// 4.将手势添加到view上
[self.yellowView addGestureRecognizer:pinch];
}
- (void)pinchYellowView:(UIPinchGestureRecognizer *)pinch {
// CGRect frame = self.yellowView.frame;
// frame.size.width *= pinch.scale;
// frame.size.height *= pinch.scale;
// self.yellowView.frame = frame;
// pinch.scale = 1;
// 使用transform
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
// 重置缩放比例
pinch.scale = 1;
NSLog(@"pinchYellowView --- %f", pinch.velocity);
}
UIRotationGestureRecognizer(旋转)
- (void)testRotate {
// 1.创建手势识别器(UIRotationGestureRecognizer(旋转))
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateYellowView:)];
// 2.设置手势属性
// 4.将手势添加到view上
[self.yellowView addGestureRecognizer:rotate];
}
- (void)rotateYellowView:(UIRotationGestureRecognizer *)rotate {
// 使用trasform
rotate.view.transform = CGAffineTransformRotate(rotate.view.transform, rotate.rotation);
// 重置旋转角度
rotate.rotation = 0;
}
UISwipeGestureRecognizer(轻扫)
- (void)testSwipe {
// 1.创建手势识别器(UISwipeGestureRecognizer(轻扫))
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeYellowView:)];
// 2.设置手势属性
// swipe.numberOfTouchesRequired = 1;
// 轻扫方向(只支持一个方向)
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
/*
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
};
*/
// 4.将手势添加到view上
[self.yellowView addGestureRecognizer:swipe];
}
- (void)swipeYellowView:(UISwipeGestureRecognizer *)swipe {
NSLog(@"向左边");
}
手势识别的功能都讲解完了,但是这些手势都是单独介绍的,那么这些手势能一起使用吗,答案是肯定的。
那么该如何让多个手势一起使用呢,下面我们将两个手势一起使用。
1、 把每个手势添加到view上,再给每个手势设置代理(UIGestureRecognizerDelegate)
2、 实现代理方法-(BOOL)gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:返回YES.(是否支持多个手势共存,YES:支持,NO:不支持)
这样就可以将多个手势添加到一个view上了。