handle taps in two different points at a same time via UIGestureRecognizer

走远了吗. 提交于 2020-01-05 04:56:34

问题


I have two labels in two different positions, when both labels are tapped at the same time i want another label to show a success message.

How do I accomplish this? I can recognize a single tap or double tap with one or more finger touches but this is a different scenario. Please help. I tried this, but it does not work.

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
        tapRecognizer.numberOfTapsRequired = 1;
        tapRecognizer.numberOfTouchesRequired = 2;
        tapRecognizer.delegate = self;
        [self.view addGestureRecognizer:tapRecognizer];

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (touch.view == tap2 && touch.view == tap1)
    {
        result.text = @"success";
    }
    return YES;
}

Thanks in advance.


回答1:


What you're trying to detect isn't really a single gesture.

I'd suggest adding a tap gesture recogniser to each button. The handler would:

  1. Store the time of the tap (at the moment that the handler is called)
  2. Compare this time with the time that the other button was last tapped. If the times are very similar (perhaps 0.25 secs apart), consider that they've both been tapped simultaneously and react accordingly.

Play with the time interval on a real device to find the ideal amount.

UPDATE:

A code snippet that obviously hasn't been tested in any way:

- (void)handleButton1Tap:(UITapGestureRecognizer *)sender  {
    if (sender.state == UIGestureRecognizerStateEnded)
    {
        self.button1TapTime = CACurrentMediaTime();
        [self testForSimultaneousTap];
    }
}

- (void)handleButton2Tap:(UITapGestureRecognizer *)sender  {
    if (sender.state == UIGestureRecognizerStateEnded)
    {
        self.button2TapTime = CACurrentMediaTime();
        [self testForSimultaneousTap];
    }
}

- (void)testForSimultaneousTap
{
    if (fabs(self.button1TapTime - self.button2TapTime) <= 0.2)
    {
        // Do stuff
    }
}

where self.button1TapTime and self.button2TapTime are member variables (doubles).

Tim




回答2:


Formally I had accepted termes's answer first and that worked too, but I have found a more simpler solution to this process. There is no need for two gesture recognizers, it is achievable with a simple tap gesture recognizer with number of touches count to two. Here is the code:

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    tapRecognizer.numberOfTapsRequired = 1;
    tapRecognizer.numberOfTouchesRequired = 2;
    tapRecognizer.delegate = self;
    [self addGestureRecognizer:tapRecognizer];

Now, in the handle tap method we can easily get the two touch points by "locationOfTouch:inView:", a instance method of UIGestureRecognizer class. So in the handleTap: method we need to check if the two touch points are in the desired location. Here is the code:

-(void)handleTap:(UITapGestureRecognizer*)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateEnded)
    {
        CGPoint point1 = [recognizer locationOfTouch:0 self];
        CGPoint point2 = [recognizer locationOfTouch:1 self];

        if ([self validateTapIn:point1 and:point2])
        {
            resultLabel.text = @"success";
        }
    }
}

-(BOOL)validateTapIn:(CGPoint)point1 and:(CGPoint)point2
{
    return
    (CGRectContainsPoint(label1.frame, point1) && CGRectContainsPoint(label2.frame,:point2)) ||
    (CGRectContainsPoint(label1.frame, point2) && CGRectContainsPoint(label2.frame, point1));
}


来源:https://stackoverflow.com/questions/12708184/handle-taps-in-two-different-points-at-a-same-time-via-uigesturerecognizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!