手势识别

08-手势识别(点按,长按,轻扫)

通过touches方法监听view触摸事件的缺点?

 1.必须得自定义view,在自定义的View当中去实现touches方法. 
 2.由于是在view内部的touches方法中监听触摸事件,因此默认情况下,方法让其他外界对象监听view的触摸事件 
 3.不容易区分用户的具体手势,(不容易区分是点按手势,还是缩放手势)这些等.

iOS 3.2之后,苹果推出了手势识别功能(Gesture Recognizer在触摸事件处理方面大大简化 了开发者的开发难度.

UIGestureRecognizer手势识别器

利用UIGestureRecognizer,能轻松识别用户在某个view上做的一些常见手势   UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势

手势使用方法

1.创建手势 
2.添加手势 
3.实现手势方法

添加点按手势

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];

手势也可以设置代理

tap.delegate = self;

添加手势

[self.imageV addGestureRecognizer:tap];

代理方法:

是否允许接收手指
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
//让图形的左边不可以点击, 获取当前手指所在的点.是在图形的左边还是在图形的右边.
CGPoint curP = [touch locationInView:self.imageV];
if (curP.x > self.imageV.bounds.size.width * 0.5) {
//在图形的右侧
    return YES;
}else{
//在图形的左侧
    return NO; 
    }
return YES; 
}

添加长按手势

UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longP:)];
[self.imageV addGestureRecognizer:longP];
//当长按时调用.
//这个方法会调用很多次, 当手指长按在上面不松,来回移动时,会持续调用. 所以要判断它的状态.
- (void)longP:(UILongPressGestureRecognizer *)longP{
    if(longP.state == UIGestureRecognizerStateBegan){ 
    NSLog(@"开始长按");
    }else if(longP.state == UIGestureRecognizerStateChanged{
    NSLog(@"长按时手指移动");
    }else if(longP.state == UIGestureRecognizerStateEnded){
    NSLog(@"手指离开屏幕"); 
        }
}

添加轻扫手势

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
//轻扫手势默认是向右边称轻扫
//可以设置轻扫的方法. 一个轻扫手势只能设置一个方法法的轻扫.想要让它有多个方向的手势,必须得要设置的
swipe.direction =  UISwipeGestureRecognizerDirectionLeft;
[self.imageV addGestureRecognizer:swipe];
添加轻扫手势
UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]; 
//轻扫手势默认是向右边称轻扫
//可以设置轻扫的方法.一个轻扫手势只能设置一个方法的轻扫.想要让它有多个方向的手势,必须得要设置的
swipe2.direction =  UISwipeGestureRecognizerDirectionUp;
[self.imageV addGestureRecognizer:swipe2];

- (void)swipe:(UISwipeGestureRecognizer *)swipe{ 
//判断的轻扫的方向
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft){
    NSLog(@"向左轻扫"); 
}else if(swipe.direction ==UISwipeGestureRecognizerDirectionUp){
    NSLog(@"向上轻扫"); }
}

手势识别(拖动,旋转,捏合)

添加平移手势

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.imageV addGestureRecognizer:pan]; 

//当手指拖动时调用
- (void)pan:(UIPanGestureRecognizer *)pan{
//拖动手势也有状态
if(pan.state == UIGestureRecognizerStateBegan){ 
//开始拖动
}else if(pan.state == UIGestureRecognizerStateChanged){ 
//可能获取不到当前手指移动的举例
//是相对于最原始的点
CGPoint transP = [pan translationInView:self.imageV];
//会清空上一次的形变
   self.imageV.transform = CGAffineTransformMakeTranslation(transP.x,transP.y);
   self.imageV.transform = CGAffineTransformTranslate(self.imageV.transform, transP.x, transP.y);
//复位,让它相对于上一次.
[pan setTranslation:CGPointZero inView:self.imageV];
}else if(pan.state == UIGestureRecognizerStateEnded){ 
结束拖动
} }

添加捏合手势

UIPinchGestureRecognizer *pinGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinGes:)]; 

//设置代理使其能够同时支持多个手势
pinGes.delegate = self;
[self.imageV addGestureRecognizer:pinGes];
缩放时调用 
- (void)pinGes:(UIPinchGestureRecognizer *)pin{
self.imageV.transform =
CGAffineTransformScale(self.imageV.transform, pin.scale,pin.scale);
复位
[pin setScale:1];
}

添加旋转手势

 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
//设置代理使其能够同时支持多个手势
 rotation.delegate = self;
 [self.imageV addGestureRecognizer:rotation];

当手指开始旋转时调用.
- (void)rotation:(UIRotationGestureRecognizer *)rotation{
    self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, rotation.rotation);
//复位.
    [rotation setRotation:0];
}
原文地址:https://www.cnblogs.com/zhoudaquan/p/5037422.html