手势识别之轻点

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong)UIImageView *ImgView;

@end

@implementation ViewController



- (void)viewDidLoad {

    [super viewDidLoad];

    self.ImgView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img.jpg"]];

    self.ImgView.tag=10001;

    self.ImgView.frame=CGRectMake(100, 100, 100, 100);

    //UIImageView默认不支持用户交互 改为YES

    self.ImgView.userInteractionEnabled=YES;

    //Tap、pinch,pan、swipe只是一个简单的单个触摸,为了实现多触摸,需要设置multipleTouchEnabled支持多点触摸

    self.ImgView.multipleTouchEnabled=YES;

    //实例化识别对象 并注册监听方法

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

    [self.ImgView addGestureRecognizer:singletap];

    //需要点击的次数 默认1

    singletap.numberOfTapsRequired=1;

    //同时点击的手指个数 默认1 在模拟器中模拟不出多个手指测试需要在真机

    singletap.numberOfTouchesRequired=2;

    //设置代理

    singletap.delegate=self;

    [self.view addSubview:self.ImgView];

    

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

    doubletap.numberOfTapsRequired=2;

    doubletap.numberOfTouchesRequired=1;

    doubletap.delegate=self;

    [self.ImgView addGestureRecognizer:doubletap];

    //两个手指点击一次和一个手指点击两个用户点击不清楚可能混淆

    [singletap requireGestureRecognizerToFail:doubletap];

    

}

-(void)signaltap:(UIGestureRecognizer *)gestureRecognizer

{

    //获取点击的视图

    UIView *view=gestureRecognizer.view;

    NSLog(@"%ld",view.tag);

    // 状态是枚举类型

//    UIGestureRecognizerStatePossible,

//    UIGestureRecognizerStateBegan,

//    UIGestureRecognizerStateChanged,

//    UIGestureRecognizerStateEnded,正常情况只响应这一个

//    UIGestureRecognizerStateCancelled,

//    UIGestureRecognizerStateFailed,

//    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded

    NSLog(@"手势状态:%ld",gestureRecognizer.state);

    NSLog(@"轻点");

}

//询问一个手势接收者是否应该开始执行一个触摸接收事件

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

{

    //指定响应的区域

    CGPoint currentPoint=[gestureRecognizer locationInView:self.view];

    if (CGRectContainsPoint(CGRectMake(100, 100, 50, 50), currentPoint)) {

        return YES;

    }

    return NO;

}

//询问delegate,两个手势是否同时接收消息,返回YES同时接收。返回NO,不同时接收,默认NO

//也就是说两个gesture recognizers的delegate方法只要任意一个返回YES,则这两个就可以同时识别

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    NSLog(@"识别");

    return NO;

}

//在一个手势生命周期的早期,当一个视图里面所有的手势识别都仍处理Possible状态,那么在iOS 7中gestureRecognizer:shouldRequireFailureOfGestureRecognizer: 和 gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer: 消息会发送给这所有的手势识别的委托,来配对这个视图里的手势识别。返回YES来对这个两个传入的手势进行优先排序,就是说知道另一个手势识别失败时,这个手势识别才能成功。本质上,这些委托方法会把返回的结果作为一次和永久的决定,从而对每个手势发生时多出实时决策。(这个还不太理解)

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    return NO;

}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    return NO;

}

//在toucheBegan这个方法之前,也就是在手势状态为began之前.此方法在window对象在有触摸事件发生时,调用gesture recognizer的touchesBegan:withEvent:方法之前调用,如果返回NO,则gesture recognizer不会看到此触摸事件。(默认情况下为YES).

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

{

    return YES;

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

@end

 一个gesture recognizer是不参与UIView的事件响应链的

你可能感兴趣的:(手势)