手势识别之轻点-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

手势识别之轻点

简介:

#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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章
最新文章
相关文章