很难捕获 UIButton 子类上的双击。 (捕获双击的时间延迟)

发布于 2024-09-18 03:13:24 字数 627 浏览 10 评论 0原文

我正在谈论用同一根手指在屏幕上进行两次单独的触摸,

我认为我有正确的编码。但对我来说,在 iPad 上实际生成双击几乎是不可能的。是否可以增加单击和双击之间的时间间隔,以便更容易触发。我非常快速地双击,它会将其捕获为两次单击。只有有时我足够幸运才能触发双击。我将 UIButton 项目的子类放入滚动视图中。

无论如何,我的 UIButton 子类实现:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{

 UITouch *touch = [[event allTouches] anyObject];

 NSLog(@"Touch count:%d",touch.tapCount);

 if (touch.tapCount == 1) 
 {
  //Do things for one touch
 }
 else if (touch.tapCount == 2) 
 {
  //Do things for double touch
 }
}

这是捕获事件。这就是为什么我认为我的代码是正确的,我只是找不到与 UIEvent 有关的任何内容以及决定发生多少次触摸的内容。我在不同的 UIView 中测试了同样的东西,它的工作完全符合预期。

Im talking about two separate touches on the screen with the same finger

I think i have the coding right for this. but it is almost impossible for me to actually generate a double tap on my iPad. Is it possible to increase the time interval between a single and double tap, so it is easier to trigger. I do a double tap very fast and it captures it as two single clicks. only sometimes am i lucky enough to trigger a double tap. I place my subclass of UIButton item into a scrollview.

Anyway my subclass of UIButton implements:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{

 UITouch *touch = [[event allTouches] anyObject];

 NSLog(@"Touch count:%d",touch.tapCount);

 if (touch.tapCount == 1) 
 {
  //Do things for one touch
 }
 else if (touch.tapCount == 2) 
 {
  //Do things for double touch
 }
}

This is capturing the event. and that is why i think my code is right, i just couldnt find anything that has to do with UIEvent and what determines how many touches happen. I tested this same thing in a different UIView and it worked exactly as expected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

☆獨立☆ 2024-09-25 03:13:24

您的代码同时检测视图中的两个手指,而不是手指上的快速序列(双击)。

如果您想检测双击,最简单的方法是使用手势识别器。无论在哪里设置子类,都添加以下代码:

UITapGestureRecognizer *singleFingerDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerDoubleTap:)];
singleFingerDoubleTap.numberOfTouchesRequired = 1;
singleFingerDoubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:singleFingerDTap];
[singleFingerDTap release];

并实现一个方法来处理双击:

-(void)handleSingleFingerDoubleTap:(id)sender {
  //Do stuff
}

Your code is detecting two fingers in the view at the same time, not a rapid sequence of on finger (double tap).

If you want to detect a double tap, the easiest way is to use a Gesture Recoginizer. Wherever do setup of your sublass, add this code:

UITapGestureRecognizer *singleFingerDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerDoubleTap:)];
singleFingerDoubleTap.numberOfTouchesRequired = 1;
singleFingerDoubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:singleFingerDTap];
[singleFingerDTap release];

And implement a method to hande the double tap:

-(void)handleSingleFingerDoubleTap:(id)sender {
  //Do stuff
}
沫离伤花 2024-09-25 03:13:24

您没有调用 [super TouchsBegan:touches withEvent:event]

you not calling [super touchesBegan:touches withEvent:event]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文