首页 文章

iOS的自制轻拍手势识别器

提问于
浏览
1

我想编写自己的点击手势识别器,以检测点击次数和触摸次数(我不想使用iOS点击手势识别器,因为我想稍后以其他各种方式扩展它);

我尝试了以下操作:使用第一个 motionBegin 触摸次数作为点击的 numberOfTouches ,增加 numberOfTaps ,并启动点击检测计时器以检测点击手势,如果在一段时间内没有看到新的点击

问题是人们很快就会意识到,当进行双触式轻击手势时,iOS会通过双触或两次快速触摸事件正确检测到一个 motionBegin . 我想正确的实现应该尝试检测那些紧密发生的快速触摸事件,但我想知道是否有更好的方法来实现手势识别器 .

有人知道如何实现iOS点击手势吗?

1 回答

  • 0
    1. Add UIGestureRecognizerDelegate in your .h file. like
    @interface finalScreenViewController : UIViewController <UIGestureRecognizerDelegate>
    {
    // do your stuff
    }
    
    
    2. Create a view in your viewDidLoad method (or any other method) you wanna to add the gesture in your .m file
    ex 
    
    UIView * myView=[[UIView alloc]init];
    myView.frame=CGRectMake(0,0.self.view.frame.size.width,self.view.frame.size.height);
    [self.view addSubView: myView];
    
    
    
    UITapGestureRecognizer *letterTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMethod:)];
            letterTapRecognizer.numberOfTapsRequired = 1;
            [myView addGestureRecognizer:letterTapRecognizer];
    
    
    
    3. you can get view by
    
    - (void) tapMethod:(UITapGestureRecognizer*)sender {
         UIView *view = sender.view; 
         NSLog(@"%d", view.tag);//By tag, you can find out where you had tapped. 
    }
    

相关问题