「这是我参与11月更文挑战的第25天,活动详情查看:2021最后一次更文挑战」
关于手势
如果想监听一个 view 上面的触摸事件,之前的方法:
- 自定义一个
view - 实现
view的touches方法,在方法内部实现具体处理的代码
通过 touches 方法监听 view 触摸事件,有以下几个缺点
- 必须得是自定义的
view - 由于是在
view内部的touches方法中监听触摸事件,因此默认情况下,无法让其外界对象监听view的触摸事件 - 不容易区分用户的具体手势行为(如捏合、拖拽、轻扫、长按、旋转)
关于手势(Gesture Recognizer)
- 在iOS 3.2 之后,苹果推出了手势识别功能,在触摸事件处理方面,大大简化了开发者的开发难度
UIGestureRecognizer
- 为了完成手势的识别,必须借助手势识别器--
UIGestureRecognizer - 利用
UIGestureRecognizer,能够轻松识别用户在某个view上面做的一些常见手势 UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势,具体如下:UITapGestureRecognizer(点按)UIPinchGestureRecognizer(捏合,一般用于缩放)UIPanGestureRecognizer(拖拽)UISwipeGestureRecognizer(轻扫)UIRotationGestureRecognizer(旋转)UILongPressGestureRecognizer(长按)
创建点按手势
numberOfTapsRequired: 设置点击次数
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
tap.numberOfTapsRequired = 2;
[self.imageView addGestureRecognizer:tap];
创建长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longpress:)];
[self.imageView addGestureRecognizer:longPress];
响应方法(默认会触发俩次,第一次:点击时,第二次:松开时,一般用弹起)
- (void)longpress:(UILongPressGestureRecognizer *)longpress{
if(longpress.state == UIGestureRecognizerStateEnded){
NSLog(@"%s",__func__);
}
}
创建轻扫手势
direction: 设置轻扫方向(默认方向往右)
ISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[self.imageView addGestureRecognizer:swipe];
创建旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
[self.imageView addGestureRecognizer:rotation];
响应方法(默认传递的旋律的角度都是相对于最开始的位置)
- (void)rotation:(UIRotationGestureRecognizer *)rotat{
NSLog(@"%s",__func__);
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform,rotat.rotation);
//复位
rotat.rotation = 0;
}
创建捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[self.imageView addGestureRecognizer:pinch];
响应方法
- (void)pinch:(UIPinchGestureRecognizer *)pinch{
NSLog(@"%s",__func__);
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale,pinch.scale);
//复位,缩放比例为1
pinch.scale = 1;
}
创建拖拽手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[self.imageView addGestureRecognizer:pan];
响应方法
-(void)pan:(UIPanGestureRecognizer *)pan{
NSLog(@"%s",__func__);
//获取手势的移动,也就是相对于最开始的位置
CGPoint transP = [pan translationInView:self.imageView];
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
//复位
[pan setTranslation:CGPointZero inView:self.imageView];
}
常用手势代理方法(遵循UIGestureRecognizerDelegate)
- 是否允许开始触发手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer - 是否允许同时支持多个手势,默认是不支持多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer - 是否允许接收手指的触摸点
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch