监听触摸事件的做法:
1.如果想监听一个view上面的触摸事件,之前的做法是:
1>自定义一个view
2>实现view的touches的方法,在方法内部实现具体处理代码
2.通过touches方法监听view触摸事件,有很明显的几个缺点
1>必须得自定义view
2>由于是在view内部的touches方法中监听触摸事件,默认情况下,是无法让其他外界对象监听view的触摸事件
3>不容易区分用户的具体手势行为
iOS 3.2之后,苹果退出了手势识别功能(Gesture Recognizer),在触摸手势处理方面,大大简化了开发者的开发难度
UIGestureRecognizer---手势识别器
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体手势行为
1.UITapGestureRecognizer 点击行为
2.UIPinchGestureRecognizer 捏合行为,用于缩放
3.UIPanGestureRecognizer 拖拽行为
4.UISwipeGestureRecognizer 轻扫行为
5.UIRotationGestureRecognizer 旋转行为
6.UILongPressGestureRecognizer 长按行为
手势识别器的用法:以UITapGestureRecognizer为例
1.创建手势识别器对象 (可以直接添加代理,和执行的方法)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init] [tap addTarget:self action:]
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector()]
2.设置手势识别器对象的具体属性(一般不设置)
//连续敲击2次
tap.numberOfTapsRequired = 2;
//需要2根手指一起敲击
tap.numberOfTouchesRequired = 2;
3.添加手势识别器到对应的view上
[self.view addGestureRecognizer: tap]
手势识别的状态
typedef NS_ENUM(NSInteger,UIGestureRecognizerState)
{
//没有触摸事件发生,所有手势识别的默认状态
UIGestureRecognizerStatePossible,
//一个手势已经开始但尚未改变或者完成时
UIGestureRecognizerStateBegan,
//手势状态改变
UIGestureRecognizerStateChanged,
//手势完成
UIGestureRecognizerStateEnded,
//手势取消,恢复至Possible状态
UIGestureRecognizerStateCancelled,
//手势失败,恢复至Possible状态
UIGestureRecognizerStateFailed,
}
手势的应用(点击手势)
- (void)viewDidLoad {
[super viewDidLoad];
// 建立点击手势,并制定target ,并要求实现tap方法
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
// 将点击手势,加入_imageView的处理中, 记得一定要选择交互 , 因为默认情况下 imageView的交互为no
[_imageView addGestureRecognizer:tap];
// 设置点击手势的代理,可以做一些特定的事情
tap.delegate = self;
}
// 点击手势的范围缩减, 只接受图片一半的尺寸。
- (BOOL)gestureRecognizer:(nonnull UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(nonnull UITouch *)touch
{
CGPoint tapP = [touch locationInView:_imageView];
if (tapP.x < _imageView.frame.size.width * 0.5) {
return YES;
}
return NO;
}
// 实现点击手势,方法
- (void)tap{
NSLog(@"%s",__func__);
}
- (BOOL)gestureRecognizer:(nonnull UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(nonnull UITouch *)touch
此方法可以设置允许手势的区域 ,默认为YES.
长按和轻扫手势.
#pragma mark - <轻扫手势>
- (void)setupSwipe
{
// 多加几个轻扫手势,便能够实现多个方向的轻扫。
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe)];
[_imageView addGestureRecognizer:swipe];
// 修改轻扫的的方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe)];
[_imageView addGestureRecognizer:swipe1];
// 修改轻扫的的方向
swipe1.direction = UISwipeGestureRecognizerDirectionRight;
}
- (void)swipe
{
NSLog(@"%s",__func__);
}
#pragma mark - <长按点击>
- (void)setupLongPress
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[_imageView addGestureRecognizer:longPress];
longPress.delegate = self;
}
- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
// 传入参数, 当前的手势, 手势状态,决定事件
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"%s",__func__);
}
}
捏合,拖拽手势
- (BOOL)gestureRecognizer:(nonnull UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer
默认为NO, 返回YES是允许多重手势.
// 允许组合手势
- (BOOL)gestureRecognizer:(nonnull UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
#pragma mark - <旋转>
- (void)setupRotation
{
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
[_imageView addGestureRecognizer:rotation];
rotation.delegate = self;
}
- (void)rotation:(UIRotationGestureRecognizer*)rotation
{
CGFloat rotationF = rotation.rotation;
_imageView.transform = CGAffineTransformRotate(_imageView.transform, rotationF);
rotation.rotation = 0;
}
#pragma mark - <捏合>
- (void)setupPinch
{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[_imageView addGestureRecognizer:pinch];
pinch.delegate = self;
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
// 获取捏合比例
CGFloat scale = pinch.scale;
_imageView.transform = CGAffineTransformScale(_imageView.transform, scale, scale);
// 捏合后保持原比例,负责会变得不见得
pinch.scale = 1;
}
#pragma mark - <拖拽>
- (void)setupPan
{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[_imageView addGestureRecognizer:pan];
}
- (void)pan:(UIPanGestureRecognizer *)pan
{
CGPoint panP = [pan translationInView:_imageView];
NSLog(@"%@",NSStringFromCGPoint(panP));
// 如果不去让pan每次的tranlation为0 的话, 则再下次拖拽时 ,图片会直接回到初始位置,因为上一次的pan的translation没有清掉。
// _imageView.transform = CGAffineTransformMakeTranslation(panP.x, panP.y);
_imageView.transform = CGAffineTransformTranslate(_imageView.transform, panP.x, panP.y);
// 每次拖拽,translation 设定为0 ,可以每次根据上一次的情况定位,去实现拖拽。
[pan setTranslation:CGPointZero inView:_imageView];
}