手势识别
在练手demo中 使用UIImageView原因:之前既能看见图片,又能监听点击的只有UIButton,学了手势,我们的UIImageView也可以。
* tap(代理:左边不能点,右边能点)
* longPress(allowableMovement:触发之前,最大的移动范围)
> 默认调用两次,开始一次,结束一次。
* swipe:(一个手势只能识别一个方向)
* 旋转:
基于上一次旋转
注意:通过transform形变,需要去掉autolayout,才准确
* 复位:(手势的取值都是相对最原始的位置,我们应该是需要相对上一次,因此每次调用,就复位一下,每次都是从零开始旋转角度)
缩放:复位
* 如何同时支持旋转和缩放?默认不支持多个手指,
Simultaneously:同时
当使用一个手势的时候会调用代理的Simultaneously方法,询问是否支持多个手势
* pan
获取平移的位置:translationInView
复位:setTranslation:inView: 需要传一个view,因为点的位置跟坐标系有关系,看他是基于哪个坐标系被清空的。
@interface ViewController ()<UIGestureRecognizerDelegate>代理监听手势
#pragma mark - 手势代理方法
// 是否允许开始触发手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return NO;
}
// 是否允许同时支持多个手势,默认是不支持多个手势
// 返回yes表示支持多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
是否允许接收手指的触摸点
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
//获取当前的触摸点
CGPoint curP = [touch locationInView:self.imageView];
if (curP.x < self.imageView.bounds.size.width * 0.5) {//让imageView的右边能点
return NO;
}else{
return YES;
}
}
- (void)setUpTap
{
// 创建点按手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
tap.delegate = self;
[_imageView addGestureRecognizer:tap];
}
#pragma mark - 长按手势
// 默认会触发两次
- (void)setUpLongPress
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.imageView addGestureRecognizer:longPress];
}
#pragma mark - 清扫
- (void)setUpSwipe
{
// 默认轻扫的方向是往右
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[self.imageView addGestureRecognizer:swipe];
// 如果以后想要一个控件支持多个方向的轻扫,必须创建多个轻扫手势,一个轻扫手势只支持一个方向
// 默认轻扫的方向是往右
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
[self.imageView addGestureRecognizer:swipeDown];
}
#pragma mark - 旋转手势
- (void)setUpRotation
{
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[self.imageView addGestureRecognizer:rotation];
}
// 默认传递的旋转的角度都是相对于最开始的位置
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform,rotation.rotation);
// (self.imageView.transform, rotation.rotation);
// 复位
rotation.rotation = 0;
// 获取手势旋转的角度
NSLog(@"%f",rotation.rotation);
}
#pragma mark - 捏合
- (void)setUpPinch
{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
pinch.delegate = self;
[self.imageView addGestureRecognizer:pinch];
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
// 复位
//
pinch.scale = 1;
}
#pragma mark - 拖拽
- (void)setUpPan
{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.imageView addGestureRecognizer:pan];
}
- (void)pan:(UIPanGestureRecognizer *)pan
{
// 获取手势的触摸点
// CGPoint curP = [pan locationInView:self.imageView];
// 移动视图
// 获取手势的移动,也是相对于最开始的位置
CGPoint transP = [pan translationInView:self.imageView];
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
// 复位
[pan setTranslation:CGPointZero inView:self.imageView];
// NSLog(@"%@",NSStringFromCGPoint(curP));
}
满地打滚卖萌求赞,如果本文帮助到你,轻点下方的红心,给作者君增加更新的动力。