Skip to content

Instantly share code, notes, and snippets.

@shinriyo
Created May 13, 2013 23:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinriyo/5572375 to your computer and use it in GitHub Desktop.
Save shinriyo/5572375 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class SwipeManager : MonoBehaviour
{
private float startTime;
private Vector2 startPos;
private bool couldBeSwipe;
public float comfortZone;
public float minSwipeDist;
public float maxSwipeTime;
void Update ()
{
if (iPhoneInput.touchCount > 0) {
iPhoneTouch touch = iPhoneInput.touches [0];
switch (touch.phase) {
case iPhoneTouchPhase.Began:
couldBeSwipe = true;
startPos = touch.position;
startTime = Time.time;
break;
case iPhoneTouchPhase.Moved:
if (Mathf.Abs (touch.position.y - startPos.y) > comfortZone) {
couldBeSwipe = false;
}
break;
case iPhoneTouchPhase.Stationary:
couldBeSwipe = false;
break;
case iPhoneTouchPhase.Ended:
float swipeTime = Time.time - startTime;
float swipeDist = (touch.position - startPos).magnitude;
if (couldBeSwipe && (swipeTime < maxSwipeTime) && (swipeDist > minSwipeDist)) {
// It's a swiiiiiiiiiiiipe!
float swipeDirection = Mathf.Sign (touch.position.y - startPos.y);
// Do something here in reaction to the swipe.
Debug.Log (swipeDirection);
}
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment