Search Unity

Sliding finger (touch) gesture...

Discussion in 'iOS and tvOS' started by seon, Oct 23, 2008.

  1. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    Anyone got a sliding finger (touch) gesture working and if so, wanna share some code?

    Like a slide left/slide right gesture.
     
  2. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    yes please.
     
  3. lesfundi

    lesfundi

    Joined:
    Jan 10, 2009
    Posts:
    628
    i am very interesed in that too

    lesfundi
     
  4. sigsom

    sigsom

    Joined:
    Aug 9, 2009
    Posts:
    133
    This has already been covered before and should be on the unify site.

    Here are some of my snippets
    Code (csharp):
    1.  
    2. public int deviationAmount = 20;
    3. public ArrayList trackers = new ArrayList();
    4. Hashtable trackerLookup = new Hashtable();
    5. private ArrayList ended;
    6. private TouchTracker tracker;
    7. private ArrayList movements;
    8. ....
    9. void Update () {
    10.         // clean all touches (so they know if they aren't updated after we pull info)
    11.         for(i=0;i<trackers.Count;i++)
    12.             ((TouchTracker)trackers[i]).Clean();
    13.         // track which events vanished (without using iPhoneTouchPhase.Ended)
    14.         ended = new ArrayList();
    15.             // process our touches
    16.         for(i = 0;i<iPhoneInput.touches.Length;i++)
    17.         {          
    18.             // iPhoneTouchPhase.Ended isn't very reliable (especially with the remote)
    19.             // but this is how we can tell if we got single or double finger taps
    20.             if(iPhoneInput.touches[i].phase == iPhoneTouchPhase.Ended  iPhoneInput.touches[i].tapCount>0)
    21.             {
    22.                 if(iPhoneInput.touchCount == 1)
    23.                 {
    24.                     //Debug.Log("tracker.tapCount="+touch.tapCount);
    25.                     this.HandleSingleTap();
    26.                     return;
    27.                 }
    28.                 if(iPhoneInput.touchCount == 2  iPhoneInput.touches[i].fingerId == 1)
    29.                 {
    30.                     //Debug.Log("tracker.tapCount="+touch.tapCount);
    31.                     this.HandleTwoFingerTap();
    32.                     return;
    33.                 }
    34.             }
    35.             else
    36.             {
    37.                 // try to get our tracker for this finger id
    38.                 tracker = (TouchTracker)trackerLookup[iPhoneInput.touches[i].fingerId];
    39.            
    40.                 if(tracker != null)
    41.                     tracker.Update(iPhoneInput.touches[i]);
    42.                 else
    43.                     tracker = BeginTracking(iPhoneInput.touches[i]);
    44.             }
    45.            
    46.         }
    47.         // use an intermediate list because EndTracking removes from trackers arraylist
    48.         for(i=0;i<trackers.Count;i++)
    49.         {
    50.             if(!((TouchTracker)trackers[i]).isDirty)
    51.                 ended.Add(trackers[i]);
    52.         }
    53.         movements = new ArrayList();
    54.         for(i=0;i<ended.Count;i++)
    55.         {
    56.             movements.Add(EndTracking((TouchTracker)ended[i]));
    57.         }
    58.         if(movements.Count == 0)
    59.             return;
    60.        
    61.         //Debug.Log("movements.Count="+movements.Count);
    62.         //single finger swipe and only do it if we have finished all trackers
    63.         if(movements.Count == 1  trackers.Count==0)
    64.         {
    65.             this.HandleSingleSwipe((Vector2)movements[0]);
    66.         }
    67.         //two finger swipe and only do it if we have finished all trackers
    68.         else if(movements.Count == 2  trackers.Count==0)
    69.         {
    70.             this.HandleTwoFingerSwipe((Vector2)movements[0],(Vector2)movements[1]);
    71.         }
    72. }
    73. ....
    74. void HandleSingleSwipe(Vector2 movement)
    75. {
    76.         if(IsSwipeRight(movement))
    77.         {
    78.             //Debug.Log("Single Finger Swipe Right");
    79.         }
    80. }
    81. ....
    82.     bool IsSwipeRight(Vector2 movement)
    83.     {
    84.         if(movement.x > deviationAmount  Mathf.Abs(movement.y) < deviationAmount)
    85.             return true;
    86.         return false;
    87.     }
    88.     TouchTracker BeginTracking(iPhoneTouch touch)
    89.     {
    90.         TouchTracker tracker = new TouchTracker(touch);
    91.    
    92.         // remember our tracker
    93.         trackers.Add(tracker);
    94.         trackerLookup[touch.fingerId] = tracker;
    95.    
    96.         return tracker;
    97.     }
    98.     Vector2 EndTracking(TouchTracker tracker)
    99.     {
    100.         Vector2 movement = tracker.End();
    101.  
    102.         trackers.Remove(tracker);
    103.         trackerLookup[tracker.fingerId] = null;
    104.         return movement;
    105.     }
    106.  
     
  5. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    Hi, here is a script I made today if you want to give it a try. Not massively tested. If you find any issues plz post I will take a look at it.


    Code (csharp):
    1. /*
    2.  
    3. Swipe gesture for iPhone. Add this script and a GUIText component to an empty GO
    4. With the GO selected in the inspector set:
    5. swipeLength // this is how long you want the swipe to be. 25 pixels seems ok
    6. swipeVariance // this is how far the drag can go 'off line'. 5 pixels either way seems ok
    7.  
    8. You can swipe as many fingers left or right and it will only pick up one of them
    9. It will then allow further swipes when that finger has been lifed from the screen.
    10. Typically its swipe > lift finger > swipe .......
    11.  
    12. Be aware that it sometimes does not pick up the iPhoneTouchPhase.Ended
    13. This is either a bug in the logic (plz test) or as the TouchPhases are notoriously
    14. inaccurate its could well be this or it could be iPhone 1.6 given that it is quirky with touches.
    15. Anyhow it does not affect the working of the class
    16. other than a dead swipe once in a while which then rectifies itself on the next swipe so
    17. no big deal.
    18.  
    19. No need for orientation as it will respect whatever you set.
    20. */
    21.  
    22. using UnityEngine;
    23. using System.Collections;
    24.  
    25. public class Swipe : MonoBehaviour {
    26.  
    27. //public member vars
    28. public int swipeLength;
    29. public int swipeVariance;
    30. //private member vars
    31. private GUIText swipeText;
    32. private Vector2[] fingerTrackArray;
    33. private bool[] swipeCompleteArray;
    34. private int activeTouch = -1;
    35.  
    36. //methods
    37.     void Start()
    38.     {
    39.         //get a reference to the GUIText component
    40.         swipeText = (GUIText) GetComponent(typeof(GUIText));
    41.         fingerTrackArray = new Vector2[5];
    42.         swipeCompleteArray = new bool[5];
    43.     }  
    44.  
    45.     void Update()
    46.     {
    47.         //touch count is a mess at the moment so add the extra check to see if there are no more than 5 touches
    48.         if(iPhoneInput.touchCount > 0  iPhoneInput.touchCount < 6)
    49.         {
    50.             foreach(iPhoneTouch touch in iPhoneInput.touches)
    51.             {
    52.                 if(touch.phase == iPhoneTouchPhase.Began)
    53.                 {
    54.                     fingerTrackArray[touch.fingerId] = touch.position; 
    55.                 }
    56.                 //check if withing swipe variance      
    57.                 if(touch.position.y > (fingerTrackArray[touch.fingerId].y + swipeVariance))
    58.                     fingerTrackArray[touch.fingerId] = touch.position;
    59.                 if(touch.position.y < (fingerTrackArray[touch.fingerId].y - swipeVariance))
    60.                     fingerTrackArray[touch.fingerId] = touch.position;
    61.                 //swipe right
    62.                 if((touch.position.x > fingerTrackArray[touch.fingerId].x + swipeLength)  !swipeCompleteArray[touch.fingerId]
    63.                      activeTouch == -1)
    64.                 {
    65.                     activeTouch = touch.fingerId;
    66.                     //Debug.Log(touch.fingerId + " " + fingerTrackArray[touch.fingerId] + " " +  touch.position);
    67.                     swipeCompleteArray[touch.fingerId] = true;
    68.                     SwipeComplete("swipe right  ",  touch);
    69.                 }
    70.                 //swipe left
    71.                 if((touch.position.x < fingerTrackArray[touch.fingerId].x - swipeLength)  !swipeCompleteArray[touch.fingerId]
    72.                      activeTouch == -1)
    73.                 {
    74.                     activeTouch = touch.fingerId;
    75.                     //Debug.Log(touch.fingerId + " " + fingerTrackArray[touch.fingerId] + " " +  touch.position);
    76.                     swipeCompleteArray[touch.fingerId] = true;
    77.                     SwipeComplete("swipe left  ", touch);
    78.                 }
    79.                 //when the touch has ended we can start accepting swipes again
    80.                 if(touch.fingerId == activeTouch  touch.phase == iPhoneTouchPhase.Ended)
    81.                 {
    82.                     //Debug.Log("Ending " + touch.fingerId);
    83.                     //if more than one finger has swiped then reset the other fingers so
    84.                     //you do not get a double/triple etc. swipe
    85.                     foreach(iPhoneTouch touchReset in iPhoneInput.touches)
    86.                     {
    87.                         fingerTrackArray[touchReset.fingerId] = touchReset.position;   
    88.                     }
    89.                     swipeCompleteArray[touch.fingerId] = false;
    90.                     activeTouch = -1;
    91.                 }
    92.             }          
    93.         }  
    94.     }
    95.    
    96.     void SwipeComplete(string messageToShow, iPhoneTouch touch)
    97.     {
    98.        
    99.         swipeText.text = messageToShow;
    100.         //Debug.Log("doing something");
    101.         //Do something here
    102.     }
    103.    
    104.  
    105. }
    106.  
    If you find it works ok plz let me know. Also if you want to enter a timer to check swipe against slow drag it should be pretty easy.
     
  6. minevr

    minevr

    Joined:
    Mar 4, 2008
    Posts:
    1,018
    Take a look,thanks for share~~ :p
     
  7. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    Anyone have something in JS? Only seen C# so far, and I have no idea how to implement into my JS-based App.
     
  8. Bugfoot

    Bugfoot

    Joined:
    Jan 9, 2009
    Posts:
    533
  9. amitchai

    amitchai

    Joined:
    Nov 8, 2010
    Posts:
    12
    i am using this code for a slide arrow to load the menu:

    if (Input.GetTouch(i).phase == TouchPhase.Moved BackTouch)
    {

    backArrow.transform.position.x += 2;
    if(touch.position.x > pressedLocation+Screen.width*0.1)
    {
    backArrow.transform.position.x = backArrowOriginalPos;
    Start();
    click.audio.Play();
    Application.LoadLevel (0);
    }
    }
    if (Input.GetTouch(i).phase == TouchPhase.Ended BackTouch)
    {
    backArrow.transform.position.x = backArrowOriginalPos;
    BackTouch = false;
     
  10. deadfire52

    deadfire52

    Joined:
    Jun 15, 2011
    Posts:
    101
    Here's what I've been using, it's CS but I used it in my JS project since the action is pretty simple it works fine. Not my own work but I edited some of it:

     
    Last edited: Aug 5, 2012
  11. DeveshPandey

    DeveshPandey

    Joined:
    Sep 30, 2012
    Posts:
    221