监听触摸事件的做法
如果想监听一个view上面的触摸事件,之前的做法是
- 自定义一个view
- 实现view的touches方法,在方法内部实现具体处理代码
通过touches方法监听view触摸事件,有很明显的几个缺点
- 必须得自定义view
- 由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件
- 不容易区分用户的具体手势行为
iOS 3.2之后,苹果推出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度
UIGestureRecognizer
为了完成手势识别,必须借助于手势识别器—-UIGestureRecognizer
利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
- UITapGestureRecognizer(敲击)
- UIPinchGestureRecognizer(捏合,用于缩放)
- UIPanGestureRecognizer(拖拽)
- UISwipeGestureRecognizer(轻扫)
- UIRotationGestureRecognizer(旋转)
- UILongPressGestureRecognizer(长按)
UITapGestureRecognizer点击
每一个手势识别器的用法都差不多,比如UITapGestureRecognizer的使用步骤如下
创建手势识别器对象
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
设置手势识别器对象的具体属性
// 连续敲击2次
tap.numberOfTapsRequired = 2;
// 需要2根手指一起敲击
tap.numberOfTouchesRequired = 2;
添加手势识别器到对应的view上
[self.iconView addGestureRecognizer:tap];
监听手势的触发
[tap addTarget:self action:@selector(tapIconView:)];
设置UIImageView可以点击,且只能左边可以点击
// 点按
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
tap.delegate = self;
[_imageView addGestureRecognizer:tap];
// 是否允许接收手指
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
// 获取手指的触摸点
CGPoint curP = [touch locationInView:self.imageView];
// 左边能点
if (curP.x < self.imageView.bounds.size.width * 0.5) {
return YES;
}else{
return NO;
}
}
UILongPressGestureRecognizer长按
- (void)setUpLongPress
{
// 长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.imageView addGestureRecognizer:longPress];
}
// 长按图片的时候就会触发长按手势 会调用多次
- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
// 一般开发中,长按操作只会做一次
// 假设在一开始长按的时候就做一次操作
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"%ld",longPress.state);
}
}
UISwipeGestureRecognizer轻扫
注意:一个手势对象对应一个手势,一个控件可以同时添加多个手势
// 轻扫
- (void)setUpSwipe
{
// 轻扫
// 一个手势只能支持一个方向
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
// 轻扫方向:默认是右边
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[self.imageView addGestureRecognizer:swipe];
UISwipeGestureRecognizer *swipeD = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
// 轻扫方向:下边
swipeD.direction = UISwipeGestureRecognizerDirectionDown;
[self.imageView addGestureRecognizer:swipeD];
}
- (void)swipe:(UISwipeGestureRecognizer *)swipe
{
if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(@"下");
}else{
}
NSLog(@"%s",__func__);
}
UIPanGestureRecognizer拖拽
注意:复位
// 拖拽
- (void)setUpPan
{
// 拖拽
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.imageView addGestureRecognizer:pan];
}
- (void)pan:(UIPanGestureRecognizer *)pan
{
// 返回的是相对于最原始的手指的偏移量
CGPoint transP = [pan translationInView:self.imageView];
NSLog(@"%@",NSStringFromCGPoint(transP));
// 移动图片控件
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
// 复位,表示相对上一次
[pan setTranslation:CGPointZero inView:self.imageView];
}
UIRotationGestureRecognizer旋转
注意复位
// 旋转
- (void)setUpRotation
{
// 旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[self.imageView addGestureRecognizer:rotation];
}
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
NSLog(@"%f",rotation.rotation);
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
// 复位
rotation.rotation = 0;
}
UIPinchGestureRecognizer捏合(放大缩小)
注意复位
- (void)setUpPinch
{
// 捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
pinch.delegate = self;
[self.imageView addGestureRecognizer:pinch];
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
NSLog(@"%f",pinch.scale);
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
// 复位
pinch.scale = 1;
}
默认状态下,一个控件只能支持一种手势,当需要同时支持多个手势时,比如:同时旋转,放大缩小,需要设置手势的代理方法
// Simultaneously:同时
// 是否允许同时支持多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}