UIGestureRecognizer
- 为了完成手势识别,必须借助于手势识别器————UIGestureRecognizer
- 利用UIGestureRecognizer,能轻松识别用户在某个View上面做的一些常见手势
- UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
1)UITapGestureRecognizer(敲击)
2)UILongPressGestureRecognizer(长按)
3)UIPinchGestureRecognizer(捏合,用于缩放)
4)UIPanGestureRecognizer(拖拽)
5)UISwipeGestureRecognizer(轻扫)
6)UIRotationGestureRecognizer(旋转)
手势的代理
tap.delegate = self;
代理方法
//是否允许接收手指
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizershouldReceiveTouch:(UITouch *)touch{
//获取当前点的位置
CGPoint curP = [touch locationInView:self.imageV];
return YES;
}
eg:
点按(敲击)手势
@property(nonatomic , weak) UIImage *imageV;
//1.创建手势(敲击)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
//2.添加手势
[self.imageV addGestureRecognizer:tap];
//3.实现手势方法
-(void)tap{
NSLog(@"%s",__func__);
}
长按手势
- 在长按手势中,有一些状态(state属性,这是一个枚举属性)
//1.创建长按手势
UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longP:)];
//2.添加手势
[self.imageV addGestureRecognizer:longP];
//3.实现长按方法(在使用长按手势时,长按并且移动会持续调用方法)
-(void)longP:(UILongPressGestureRecognizer *)longP{
//🌟判断手势状态
if(longP.state==UIGestureRecognizerStateBegan){
NSLog(@"开始长按");
}
}
轻扫手势
- 在轻扫手势中可以设置轻扫的方向(direction枚举属性)
//1.创建手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
//2.设置轻扫的方向(一个轻扫手势只能有一个轻扫方向)
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
//3.添加手势
[self.imageV addGestureRecognizer:swipe];
//4.实现轻扫方法
-(void)swipe:(UISwipeGestureRecognizer *)swipe{
if(swipe.direction == UISwipeGestureRecognizerDirectionLeft){
NSLog(@"向左轻扫");
}
}
需要复位的手势⬇️
拖动手势(光标跟着鼠标移动)
//1.添加拖动手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selecct(pan:)];
//2,添加拖动手势
[self.imageV addGestureRecognizer:pan];
//3.实现拖动方法
-(void)pan:(UIPangestureRecognizer *)pan{
//获取偏移量(相对于原始)
CGPoint transP = [pan translationInView:self.image];
self.imageV.transform = CGAffineTransformTranslate(self.imageV.transform,transP.x,transp.y);
//相对于上一次
//偏移量复位
[pan setTranslation:CGPointZero inView:self.imageV];
}
旋转
//1.创建旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:@selector:(rotationGes:)];
//2.添加旋转手势
[self.imageV addGestureRecognizer:rotation];
//3.实现手势方法(rotation属性:获取角度)
-(void)rotationGes:(UIRotationGestureRecognizer *)rotationGes{
self.imageV.transform = CGAffineTransformRotate(self.imageV.transform,rotationGes.rotation);
//复位
[rotationGes setRotation:0];
}
缩放
//1.创建缩放手势
UIPinchGestureRecognizer *pinch = [[UIPinckGestureRecognizer alloc] initWithTarget:@select(pinch:)];
//2.添加缩放手势
[self.imageV addGestureRecognizer:pinch];
//3.实现缩放方法(Scale属性:控制缩放比例)
-(void)pinch:(UIPinchGestureRecognizer *)pinch{
self.imageV.transform = CGAffineTransformScale(self.imageV.transform,pinch.scale,pinch.scale);
//复位
[pinch setScale:1];
}
同时支持多个手势运行
通过代理,实现代理方法
- 千万别写错了方法名字
- 不要忘记设置代理:例如旋转手势设置代理
rotation.delegate = self; - 不要忘记设置代理协议
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
566


&spm=1001.2101.3001.11974&articleId=105886707&d=1&t=3&u=cde0b0ae65964869b66d58b92fd99513)

被折叠的 条评论
为什么被折叠?



