Author Topic: Event for Drag ended?  (Read 6834 times)

ronronmx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Event for Drag ended?
« on: April 16, 2013, 11:09:46 PM »
Hey guys,
I use the "OnDrag" Event Listener to detect when I'm dragging on a UI element (not drag & drop, just dragging over an area), and I need to know when the Drag ends, for example, when the user lifts its finger. I know about the "OnDrop" and "OnDragFinished" events, but those are part of the UIEventListener class like the "OnDrag" one, and are mostly for detecting when the user drops whatever object he was "moving" correct?

Here's my code:
  1. UIEventListener.Get( preloadBtn ).onDrag += CheckForPreloadNGUI;
  2.  
  3. // The behavior I need is this:
  4. UIEventListener.Get( preloadBtn ).onDragEnded += CancelPreloadNGUI;
  5.  

There is no "OnDragEnded" or "OnDragFinished" that use the "UIEventListener" class, so how can I achieve this? Do I have to use the "OnPress" event to detect when the Drag ends?

Thanks in advance!
Stephane

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Event for Drag ended?
« Reply #1 on: April 17, 2013, 03:36:15 AM »
Here's a way:
  1. bool wasDragging = false;
  2.  
  3. void OnPress(bool pressed)
  4. {
  5. if (!pressed && wasDragging)
  6. {
  7. wasDragging = false;
  8. //do onDragEnded stuff
  9. }
  10. }
  11.  
  12. void OnDrag(Vector2 delta)
  13. {
  14. wasDragging = true;
  15. }
  16.  
  17.  

ronronmx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Event for Drag ended?
« Reply #2 on: April 17, 2013, 10:48:20 AM »
Here's a way:
  1. bool wasDragging = false;
  2.  
  3. void OnPress(bool pressed)
  4. {
  5. if (!pressed && wasDragging)
  6. {
  7. wasDragging = false;
  8. //do onDragEnded stuff
  9. }
  10. }
  11.  
  12. void OnDrag(Vector2 delta)
  13. {
  14. wasDragging = true;
  15. }
  16.  
  17.  

Hey Nicki,
Thanks for the example, should work like a charm :)

I'll implement it when I get back home tonight and give it a try.

Stephane