简体   繁体   English

适用于iOS的自制点击手势识别器

[英]Homemade tap gesture recognizer for iOS

I'd like to code my own tap gesture recognizer, to detect the number of taps and number of touches (I don't want to use the iOS tap gesture recognizer because I want to extend it later in various other manners) ; 我想编写自己的敲击手势识别器,以检测敲击次数和触摸次数(我不想使用iOS的敲击手势识别器,因为我想稍后以各种其他方式对其进行扩展);

I tried the following : use the first motionBegin number of touches as the numberOfTouches of the tap, increment the numberOfTaps , and start the tap detection timer to detect the tap gesture if no new taps has been seen in a while 我试过如下:使用第一个motionBegin触摸的数量numberOfTouches水龙头,递增numberOfTaps ,如果没有新的水龙头已经在一段时间被视为启动轻击检测计时器检测敲击手势

The problem is that one quickly realises that when doing a double-touch tap gesture, iOS either correctly detects one motionBegin with a double touch, or two quick one touch events. 问题在于,人们很快意识到,在执行双击触摸手势时,iOS可以正确检测到一个带有两次触摸的motionBegin或两个快速的一次触摸事件。 I guess a correct implementation should try to detect those quick one touch events that happen closely, but I'm wondering if there is a better way to implement the gesture recognizer. 我猜想正确的实现应该尝试检测紧密发生的快速一键事件,但是我想知道是否有更好的方法来实现手势识别器。

Someone knows how the iOS tap gesture is implemented? 有人知道如何实现iOS点击手势吗?

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. 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM