使用 UIView 只检测双击或单击?

发布于 2024-10-19 16:31:29 字数 381 浏览 4 评论 0原文

我想在用户触摸视图时检测到双击/单击。

我做了这样的事情:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    CGPoint prevLoc = [touch ]
    if(touch.tapCount == 2)
        NSLog(@"tapCount 2");
    else if(touch.tapCount == 1)
        NSLog(@"tapCount 1");
}

但它总是在 2 次点击之前检测到 1 次点击。如何检测 1 / 2 次点击?

I want to detect JUST double / single tap when the user touches a view.

I made something like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    CGPoint prevLoc = [touch ]
    if(touch.tapCount == 2)
        NSLog(@"tapCount 2");
    else if(touch.tapCount == 1)
        NSLog(@"tapCount 1");
}

But it always detect 1 tap before 2 taps. How can I detect just 1 / 2 tap?

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

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

发布评论

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

评论(3

一个人练习一个人 2024-10-26 16:31:29

谢谢你的帮助。我也找到了类似的方法:

-(void)handleSingleTap
{
    NSLog(@"tapCount 1");
}

-(void)handleDoubleTap
{
    NSLog(@"tapCount 2");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSUInteger numTaps = [[touches anyObject] tapCount];
    float delay = 0.2;
    if (numTaps < 2) 
    {
        [self performSelector:@selector(handleSingleTap) withObject:nil afterDelay:delay ];     
        [self.nextResponder touchesEnded:touches withEvent:event];
    } 
    else if(numTaps == 2) 
    {
        [NSObject cancelPreviousPerformRequestsWithTarget:self];            
        [self performSelector:@selector(handleDoubleTap) withObject:nil afterDelay:delay ];
    }               
}

Thanks for you help. I also found a way like that:

-(void)handleSingleTap
{
    NSLog(@"tapCount 1");
}

-(void)handleDoubleTap
{
    NSLog(@"tapCount 2");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSUInteger numTaps = [[touches anyObject] tapCount];
    float delay = 0.2;
    if (numTaps < 2) 
    {
        [self performSelector:@selector(handleSingleTap) withObject:nil afterDelay:delay ];     
        [self.nextResponder touchesEnded:touches withEvent:event];
    } 
    else if(numTaps == 2) 
    {
        [NSObject cancelPreviousPerformRequestsWithTarget:self];            
        [self performSelector:@selector(handleDoubleTap) withObject:nil afterDelay:delay ];
    }               
}
日暮斜阳 2024-10-26 16:31:29

这将有助于定义单击和双击的方法,

(void) handleSingleTap {}
(void) handleDoubleTap {}

因此,在 touchesEnded 中,您可以根据点击次数调用适当的方法,但仅在延迟后调用handleSingleTap,以确保双击没有被执行。执行:

-(void) touchesEnded(NSSet *)touches withEvent:(UIEvent *)event {
  if ([touch tapCount] == 1) {
        [self performSelector:@selector(handleSingleTap) withObject:nil
           afterDelay:0.3]; //delay of 0.3 seconds
    } else if([touch tapCount] == 2) {
        [self handleDoubleTap];
    }
}

touchesBegan中,取消所有对handleSingleTap的请求,以便第二次tap取消第一次tap对handleSingleTap的调用,并且只调用handleDoubleTap

[NSObject cancelPreviousPerformRequestsWithTarget:self
  selector:@selector(handleSingleTap) object:nil];

It will help to define methods for single and double taps

(void) handleSingleTap {}
(void) handleDoubleTap {}

So then in touchesEnded you can call the appropriate method based on the number of taps, but only call handleSingleTap after a delay to make sure double tap has not been executed:

-(void) touchesEnded(NSSet *)touches withEvent:(UIEvent *)event {
  if ([touch tapCount] == 1) {
        [self performSelector:@selector(handleSingleTap) withObject:nil
           afterDelay:0.3]; //delay of 0.3 seconds
    } else if([touch tapCount] == 2) {
        [self handleDoubleTap];
    }
}

In touchesBegan, cancel all requests to handleSingleTap so that the second tap cancels the first tap's call to handleSingleTap and only handleDoubleTap will be called

[NSObject cancelPreviousPerformRequestsWithTarget:self
  selector:@selector(handleSingleTap) object:nil];
想念有你 2024-10-26 16:31:29

也许你可以使用一些时间间隔。等待 (x) 毫秒调度事件。如果您在该时间段内获得两次点击,请进行双击。如果您只获得一次调度,请点击一下。

Maby you can use some time interval. Wait with dispatching the event for (x)ms. If you get two taps in that time-period, dispatch a double-tap. If you only get one- dispatch single tap.

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