UIGestureRecognizer基类
是一个抽象类,我们主要是使用它的子类
列举一下我常用的几种手势识别
UIPanGestureRecognizer拖拽手势
UIPinchGestureRecognizer捏合缩放手势
UIRotationGestureRecognizer旋转手势
UITapGestureRecognizer点击手势
UILongPressGestureRecognizer长按手势
我用工程代码简单的说一下这几种手势识别器的用法在ViewDidLoad中写一个ImageView,并给它加手势识别
@interface ViewController ()<UIGestureRecognizerDelegate>
挂上代理
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
imageView.center = self.view.center;
imageView.userInteractionEnabled = YES;
[imageView setImage:[UIImage imageNamed:@"1.png"]];
[self.view addSubview:imageView];
}
如果你想要多个手势同时起作用,记得实现这个方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
第一种手势
1.Pan拖拽手势
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panImageView:)];
[imageView addGestureRecognizer:panGesture];
实现拖拽的方法
#pragma mark--拖拽
- (void)panImageView:(UIPanGestureRecognizer *)pan
{
CGPoint point = [pan translationInView:self.view];
pan.view.center = CGPointMake(pan.view.center.x+point.x, pan.view.center.y+point.y);
[pan setTranslation:CGPointZero inView:self.view];
}
效果就是图片可以随意拖动,手势停止图片马上停止在拖动最后停止的位置,如果觉得动作太生硬,可以添加一下代码,使图片在拖动停止后滑动一小段距离
/*
代码实现解析:
计算速度向量的长度。
如果速度向量小于200,那就会得到一个小于的小数,那么滑行会很短
基于速度和速度因素计算一个终点
确保终点不会跑出父View的边界
使用UIView动画使view滑动到终点
*/
#pragma mark--拖拽
- (void)panImageView:(UIPanGestureRecognizer *)pan
{
CGPoint point = [pan translationInView:self.view];
pan.view.center = CGPointMake(pan.view.center.x+point.x, pan.view.center.y+point.y);
[pan setTranslation:CGPointZero inView:self.view];
//想要拖动后有滑行的效果
if (pan.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [pan velocityInView:self.view];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 200;
float slideFactor = 0.1 * slideMult; // Increase for more of a slide
CGPoint finalPoint = CGPointMake(pan.view.center.x + (velocity.x * slideFactor),
pan.view.center.y + (velocity.y * slideFactor));
finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);
[UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
pan.view.center = finalPoint;
} completion:nil];
}
}
第二种手势
Pinch捏合缩放手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
pinch.delegate = self;
[imageView addGestureRecognizer:pinch];
实现捏合手势方法
#pragma mark--捏合
- (void)pinchView:(UIPinchGestureRecognizer *)pinch
{
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
pinch.scale = 1; // scale表示缩放比例
}
第三种手势
Rotation旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationImageView:)];
[imageView addGestureRecognizer:rotation];
旋转手势实现方法
#pragma mark--旋转
- (void)rotationImageView:(UIRotationGestureRecognizer *)rotation
{
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
rotation.rotation = 0;
}
第四种手势
Tap单击手势
UITapGestureRecognizer *SingleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageView:)];
[imageView addGestureRecognizer:SingleTapGesture];
Tap双击
手势
UITapGestureRecognizer *DoubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageView:)];
<span style="color:#cc33cc;">DoubleTapGesture.numberOfTapsRequired = 2;</span>
[imageView addGestureRecognizer:DoubleTapGesture];
点击手势实现方法,这里为了区别单击和双击效果,我在点击的时候有文本提示
#pragma mark--点击
- (void)tapImageView:(UITapGestureRecognizer *)tap
{
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
[lable setFont:[UIFont systemFontOfSize:14]];
if (tap.numberOfTapsRequired == 1) {
[lable setText:@"单击"];
}else{
[lable setText:@"双击"];
}
CGPoint point = [tap locationInView:self.view];
lable.center = point;
[self.view addSubview:lable];
[self performSelector:@selector(hideLable) withObject:self afterDelay:1];
}
在此处顺便提一下手势之间的依赖关系,比如说我现在有了单击手势了,我又想要双击手机,当我双击的时候也会相应单击手势的方法,这样就不好了,所有有了这个 [A requireGestureRecognizerToFail:B] 函数,它可以指定当A手势发生时,即便A已经滿足条件了, 也不会立刻触发 , 会等到 指定的手势B确定失败之后才触发。
所以可以这样写
[SingleTapGesturerequireGestureRecognizerToFail:DoubleTapGesture];
第五种手势
Long长按手势
UILongPressGestureRecognizer *LongGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(LongGesture:)];
[imageView addGestureRecognizer:LongGesture];
长按手势的实现方法
#pragma mark--长按
- (void)LongGesture:(UILongPressGestureRecognizer *)longGesture
{
if (longGesture.state == UIGestureRecognizerStateBegan) {
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, 30)];
[lable setFont:[UIFont systemFontOfSize:14]];
[lable setText:@"长按手势啦"];
CGPoint point = [longGesture locationInView:self.view];
lable.center = point;
[self.view addSubview:lable];
[self performSelector:@selector(hideLable) withObject:self afterDelay:1];
}
}
运行的效果: