通过touches方法监听view触摸事件有以下几个缺点
1.必须得自定义view,在自定义的View当中去实现touches方法.
2.由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件
3.不容易区分用户的具体手势行为(不容易区分是长按手势,还是缩放手势)这些等.-
iOS 3.2之后,苹果推出了手势识别功能(Gesture Recognizer在触摸事件处理方面大大简化了开发者的开发难度
UIGestureRecognizer手势识别器
利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势 注意手势有以下几种:
UITapGestureRecognizer(敲击)
UIPinchGestureRecognizer(捏合,用于缩放)
UIPanGestureRecognizer(拖拽)
UISwipeGestureRecognizer(轻扫)
UIRotationGestureRecognizer(旋转)
UILongPressGestureRecognizer(长按)
- 手势使用方法:
1.创建手势
2.添加手势
3.实现手势方法
1.添加点按手势
- 创建手势
Target:当哪对象要坚听手势
action:手势发生时调用的方法
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tap)];
手势也可以设置代理
tap.delegate = self;
添加手势
[self.imageV addGestureRecognizer:tap];
- 以下为手势代理方法:
是否允许接收手指.
当返回为yes的时候,表示能够接收手指,当为No的时候,表示不能够接收手指,也就是不能够接收事件.
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:
(UITouch *)touch{
获取当前手指所在的点
CGPoint curP = [touch locationInView:self.imageV];
if (curP.x > self.imageV.bounds.size.width * 0.5) {
在右边,返回NO
return NO;
}else{
在左边,返回yes,
return YES;
}
}
2.添加长按手势
UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(longPress:)];
添加手势
[self.imageV addGestureRecognizer:longP];
- 当手指长按时调用
注意,长按手势会调用多次,当开始长按时会调用,当长按松开时会调用,当长按移动时, 也会调用.
一般我们都是在长按刚开始时做事情,所以要判断它的状态.
这个状态是保存的当前的手势当中, 所以要把当前的长按手势传进来, 来判断当前手势的状态.
- (void)longPress:(UILongPressGestureRecognizer *)longP{
手势的状态是一个枚举UIGestureRecognizerState,可以进入头文件当中查看.
if (longP.state == UIGestureRecognizerStateBegan) {
NSLog(@"开始长按时调用");
}else if(longP.state == UIGestureRecognizerStateChanged){
会持续调用
NSLog(@"当长按拖动时调用");
}else if(longP.state == UIGestureRecognizerStateEnded){
NSLog(@"当长按松手指松开进调用");
}
}
3.轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(swipe:)];
注意:轻扫手势默认轻扫的方向是往右轻扫,可以去手动修改轻扫的方向
一个手势只能对象一个方向,想要支持多个方向的轻扫,要添加多个轻扫手势
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
添加手势
[self.imageV addGestureRecognizer:swipe];
再添加一个轻扫手势
轻扫手势
UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(swipe:)];
注意:轻扫手势默认轻扫的方向是往右轻扫,可以去手动修改轻扫的方向
一个手势只能对象一个方向,想要支持多个方向的轻扫,要添加多个轻扫手势
swipe2.direction = UISwipeGestureRecognizerDirectionDown;
添加手势
[self.imageV addGestureRecognizer:swipe2];
- 轻扫手势的方向
UISwipeGestureRecognizerDirectionRight = 1 << 0,(右)
UISwipeGestureRecognizerDirectionLeft = 1 << 1,(左)
UISwipeGestureRecognizerDirectionUp = 1 << 2,(上)
UISwipeGestureRecognizerDirectionDown = 1 << 3(下)
1.平移
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(pan:)];
添加手势
[self.imageV addGestureRecognizer:pan];
实现手势方法
手指在屏幕上移动进调用
- (void)pan:(UIPanGestureRecognizer *)pan{
获取当前手指移动的偏移量
CGPoint transP = [pan translationInView:self.imageV];
NSLog(@"%@",NSStringFromCGPoint(transP));
Make它会清空上一次的形变.
self.imageV.transform = CGAffineTransformMakeTranslation(transP.x, transP.y);
self.imageV.transform = CGAffineTransformTranslate(self.imageV.transform,
transP.x, transP.y);
复位,相对于上一次.
[pan setTranslation:CGPointZero inView:self.imageV];
}
2.旋转
添加旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]
initWithTarget:self action:@selector(rotation:)];
设置代理,设置代理的目的就让它能够同时支持旋转跟缩放
rotation.delegate = self;
添加手势
[self.imageV addGestureRecognizer:rotation];
当旋转时调用
- (void)rotation:(UIRotationGestureRecognizer *)rotation{
旋转也是相对于上一次
self.imageV.transform = CGAffineTransformRotate(self.imageV.transform,
rotation.rotation);
设置代理,设置代理的目的就让它能够同时支持旋转跟缩放
rotation.delegate = self;
也要做复位操作
rotation.rotation = 0;
}
3.添加缩放手势
添加缩放手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
[self.imageV addGestureRecognizer:pinch];
缩放手势时调用
-(void)pinch:(UIPinchGestureRecognizer *)pinch{
平移也是相对于上一次
self.imageV.transform = CGAffineTransformScale(self.imageV.transform, pinch.scale,
pinch.scale);
复位
pinch.scale = 1;
}