Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by fiercewaffle · Nov 22, 2011 at 06:01 AM · swipegesture

Swipe Gesture iOS

Ive been looking around for a script that will detect horizontal swipe gestures. I have found a few examples but they dont work. If someone can post this it would be greatly appreciated.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DeveshPandey · Nov 15, 2014 at 11:32 AM 0
Share

http://u3d.as/content/devesh-pandey/easy-gesture

Gestures:

  • Finger sliding (left,right,up and down)

  • Tap (1,2,3...n tap)

  • Pinch zoom

  • Swipe (left,right,up and down)

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by jahroy · Nov 22, 2011 at 06:36 AM

Here's some code I threw together the other day to answer a similar question.

I'm sure it will need to be altered, but it's a good start.

It works surprisingly well (but not great) when I do an iPad build.

Note:

This is just an example script intended to demonstrate the basic concept.

Here's the idea:

  • define a speed threshold: the minimum speed that determines a swipe

  • each frame that has touchCount == 1, check if the speed is greater than the threshold

  • if it is (and it wasn't the last frame) store the start position

  • if it is not (and it was the last frame) store end position and/or do stuff (swipe is complete)

Here's some tested exactly twice code:

 var  swipeThresh      :    float  =   1.2;
 var  swipeStart       :  Vector2  =   Vector2.zero;
 var  swipeEnd         :  Vector2  =   Vector2.zero;
 var  swipeWasActive   :  boolean  =   false;
 
 function Update ()
 {
     if ( Input.touchCount == 1 ) {
         processSwipe();
     }
 }
 
 function OnGUI ()
 {
     processSwipe();
     drawStartBox();
     drawEndBox();
 }
 
 function processSwipe ()
 {
     if ( Input.touchCount != 1 ) {
         return;
     }
 
     var theTouch : Touch = Input.touches[0];
 
     /* skip the frame if deltaPosition is zero */
 
     if ( theTouch.deltaPosition == Vector2.zero ) {
         return;
     }
 
     var speedVec : Vector2 = theTouch.deltaPosition * theTouch.deltaTime;
     var theSpeed :   float = speedVec.magnitude;
 
     var swipeIsActive : boolean = ( theSpeed > swipeThresh );
 
     if ( swipeIsActive ) {
 
         if ( ! swipeWasActive ) {
             swipeStart = theTouch.position;
         }
     }
 
     else {
 
         if ( swipeWasActive ) {
             swipeEnd = theTouch.position;
             Debug.Log("Swipe Complete");
         }
     }
 
     swipeWasActive = swipeIsActive;
 }
 
 function drawStartBox ()
 {
     if ( swipeStart == Vector2.zero ) {
         return;
     }
 
     /* don't forget to invert the y-coordinate */
 
     var theY = Screen.height - swipeStart.y;
     var theX = swipeStart.x;
 
     var theRect : Rect = Rect(theX, theY, 140, 40);
 
     GUI.Label(theRect, "Start");
 }
 
 function drawEndBox ()
 {
     if ( swipeEnd == Vector2.zero ) {
         return;
     }
 
     /* don't forget to invert the y-coordinate */
 
     var theY = Screen.height - swipeEnd.y;
     var theX = swipeEnd.x;
 
     var theRect : Rect = Rect(theX, theY, 140, 40);
 
     GUI.Label(theRect, "End");
 }


I just gave this a try and it works surprisingly well...

I used a speed threshhold of 1.2, which can be adjusted in the Inspector.

Hope this helps!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate object based on angle between two touch positions 2 Answers

Swipe & gesture controls for android 1 Answer

Swipe Android 1 Answer

Projection of Swipe Onto Character Plane (Perspective) 1 Answer

Help with Uniflow Gallery swipe 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges