iOS的自制轻拍手势识别器

时间:2012-09-27 10:24:53

标签: ios uigesturerecognizer gesture touches

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

我尝试了以下操作:将第一个motionBegin次触摸用作点击的numberOfTouches,增加numberOfTaps,然后启动点击检测计时器以检测点按手势一段时间内没有看到新的水龙头

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

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

1 个答案:

答案 0 :(得分: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. 
}