Author Topic: Distinguishing between "tap" (click) and "tap and hold" (press)  (Read 6906 times)

nicolethenerd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Distinguishing between "tap" (click) and "tap and hold" (press)
« on: January 14, 2014, 12:19:38 PM »
I want to distinguish between Tap events and Tap and Hold events in a way that the Tap and Hold events take precedence - basically, if the user holds their finger down anywhere on the screen (including on a button, which may have an associated OnClick event), a particular event should fire (and the OnClick handler for that button would be ignored), but if they just tap quickly on a button, the OnClick event should fire.  I am thinking of implementing something like this:

- A giant invisible sprite which fills the screen and has an OnPress Handler
- Various buttons on the screen that have OnClick handlers

When the user taps the screen, the OnPress handler should fire and test if the tap duration is longer than a certain threshold.  If it is, it should do whatever I want it to do when the user Taps and Holds (in this case, display some additional UI elements).  If it is less than the threshold, it is just a Tap, and the event handling should fall through to the OnClick handler of whatever button the user tapped.

Is this possible?  This is sort of the opposite of the question I asked last week, where I wanted the OnClick events to take precedence and catch any taps/clicks that fell through - now, I want to first test whether the user is pressing their finger down and let that take precedence, and only fire the OnClick events in the case of a short tap.

ivomarel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 53
    • View Profile
Re: Distinguishing between "tap" (click) and "tap and hold" (press)
« Reply #1 on: January 14, 2014, 07:09:29 PM »
The OnDrag method is based on drag distance (thresholds can be set in UICamera) and unfortunately not on time.

I suppose you could just create a script that stores the onPressTime and onReleaseTime and calculates the deltatime.

  1. float pressTime;
  2. float tressHold = 1.5f; // tressHold = 1.5 seconds
  3.  
  4. void OnPress (bool isPressed)
  5. {
  6.    if (isPressed){
  7.       print ("Pressed");
  8.       pressTime = Time.time;
  9.    }
  10.    else {
  11.       print ("Released");
  12.       float deltaTime = Time.time - pressTime;
  13.       if (deltaTime > tressHold) {
  14.          // You do whatever
  15.       }
  16.    }
  17. }
  18.  

nicolethenerd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Distinguishing between "tap" (click) and "tap and hold" (press)
« Reply #2 on: January 15, 2014, 09:12:10 AM »
Thanks.  Unfortunately, that doesn't really answer my question - the code you have written is what I was already trying to describe above - I guess I wasn't clear.

What I want to know is, is there a way, in the place where you have commented "you do whatever", to make the event "fall through" to any OnClick handlers that may be on a button underneath my giant sprite with the OnPress handler - rather than consuming the event in the OnPress.

(Or whether there's a completely different and better way to accomplish the same effect, without using a giant invisible sprite with an OnPress handler)

Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Distinguishing between "tap" (click) and "tap and hold" (press)
« Reply #3 on: January 15, 2014, 09:30:20 PM »
Don't rely on OnClick in your case. Trigger whatever you'd trigger in OnClick yourself.

nicolethenerd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Distinguishing between "tap" (click) and "tap and hold" (press)
« Reply #4 on: January 16, 2014, 03:12:01 PM »
Sorry, trigger it how?  I have a game world full of clickable objects with OnClick events on them - are you saying I can't use NGUI events for any of them anymore if I want to have a tap/hold behavior on the screen that takes precedence?  That's sort of what I thought, but just want to confirm.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Distinguishing between "tap" (click) and "tap and hold" (press)
« Reply #5 on: January 16, 2014, 10:30:41 PM »
Yes. NGUI events are sent as-is. In your case you are trying to insert additional logic that basically validates the click, and cancels it if certain conditions are not met.

This implies that you should not be using OnClick, as its functionality is constant. If you were using UIButton's On Click notification to trigger function ABC() before, write your own "on click" logic, and call your ABC() function directly.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Distinguishing between "tap" (click) and "tap and hold" (press)
« Reply #6 on: January 17, 2014, 04:23:02 AM »
OnClick is automated with internal logic set in the UICamera on how to interpret a Click.

If you want to do your own logic, use the OnPress like the others have suggested. Expanding a little bit on the code that ivo posted:

  1. float pressTime;
  2. float threshold = 0.5f; // 0.5 seconds
  3. void OnPress (bool pressed)
  4. {
  5.    Debug.Log("Pressed: " + pressed.ToString();
  6.    if (pressed){  
  7.       pressTime = Time.realtimeSinceStartup;
  8.       StartDraggingLogic();
  9.    }
  10.    else {
  11.       float deltaTime = Time.realtimeSinceStartup - pressTime;
  12.       if (deltaTime > threshold ) {
  13.        CancelDragging();
  14.        MyClick();
  15.       }
  16.    }
  17. }
  18.  
  19. void StartDraggingLogic() {}
  20. void CancelDragging() {}
  21. void MyClick(){}
  22.  
  23.