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

    通过touches方法监听view触摸事件有以下几个缺点
    1.必须得自定义view,在自定义的View当中去实现touches方法.
    2.由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件
    3.不容易区分用户的具体手势行为(不容易区分是长按手势,还是缩放手势)这些等.
    
    iOS 3.2之后,苹果推出了手势识别功能(Gesture Recognizer在触摸事件处理方面大大简化了开发者的开发难度
    
    UIGestureRecognizer手势识别器
    利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势
    UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
    
    注意手势有以下几种:
    UITapGestureRecognizer(敲击)
    UIPinchGestureRecognizer(捏合,用于缩放)
    UIPanGestureRecognizer(拖拽)
    UISwipeGestureRecognizer(轻扫)
    UIRotationGestureRecognizer(旋转)
    UILongPressGestureRecognizer(长按)

    手势使用方法:
        1.创建手势
        2.添加手势
        3.实现手势方法
    1.添加点按手势
        创建手势
        Target:当哪对象要坚听手势
        action:手势发生时调用的方法
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                                        action:@selector(tap)];
        手势也可以设置代理
        tap.delegate = self;
        添加手势
        [self.imageV addGestureRecognizer:tap];
    
        以下为手势代理方法:
        是否允许接收手指.
        当返回为yes的时候,表示能够接收手指,当为No的时候,表示不能够接收手指,也就是不能够接收事件.
        -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:
        (UITouch *)touch{
            获取当前手指所在的点
           CGPoint curP =  [touch locationInView:self.imageV];
            if (curP.x > self.imageV.bounds.size.width * 0.5) {
                在右边,返回NO
                return NO;
            }else{
                 在左边,返回yes,
                return YES;
            }
        }
        
        当手指开始点击时调用
        -(void)tap{
            NSLog(@"%s",__func__);
        }

    2.添加长按手势
        UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc] 
                                    initWithTarget:self action:@selector(longPress:)];
        添加手势
        [self.imageV addGestureRecognizer:longP];

        当手指长按时调用
        注意,长按手势会调用多次,当开始长按时会调用,当长按松开时会调用,当长按移动时, 也会调用.
        一般我们都是在长按刚开始时做事情,所以要判断它的状态.
        这个状态是保存的当前的手势当中, 所以要把当前的长按手势传进来, 来判断当前手势的状态.
        
        - (void)longPress:(UILongPressGestureRecognizer *)longP{
            手势的状态是一个枚举UIGestureRecognizerState,可以进入头文件当中查看.
            if (longP.state == UIGestureRecognizerStateBegan) {
                NSLog(@"开始长按时调用");
            }else if(longP.state == UIGestureRecognizerStateChanged){
                会持续调用
                NSLog(@"当长按拖动时调用");
            }else if(longP.state == UIGestureRecognizerStateEnded){
                NSLog(@"当长按松手指松开进调用");
            }
            
        }

    3.轻扫手势
        
        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 = UISwipeGestureRecognizerDirectionDown;
        添加手势
        [self.imageV addGestureRecognizer:swipe2];