/// <summary>It detect the swipe even if you keep your finger stuck to the screen. /// It doesn't consider the movement speed of the finger ///</summary> public override bool DetectSwipe(ref Touch oldTouch, ref Touch newTouch, SwipeDirection direction, OnSwipe doSwipe, float distance = -1) { if (isTouchGood(oldTouch, newTouch)) { //I need to copy because I can't pass ref to delegates Touch newTouchCopy = newTouch; Touch oldTouchCopy = oldTouch; OnSwipe doUpdateLocalTouch = delegate(SwipeDirection dir) { oldTouchCopy.position = newTouchCopy.position; oldLocalTouches[(int)direction] = oldTouchCopy; }; StupidSwipeDetector stupidDetector = new StupidSwipeDetector(); /*The oldPosition is updated at step of 1f. In this way the swipe can be detected * many time without taking out the finger from the screen * I update only the local copy, in this way it's possible to detect opposite swipes simultaneously */ stupidDetector.DetectSwipe(ref oldTouch, ref newTouch, (SwipeDirection)(-(int)direction), doUpdateLocalTouch, 1f); if (oldTouchCopy.position != newTouch.position) { OnSwipe doUpdateRefTouch = delegate(SwipeDirection dir) { oldTouchCopy.position = newTouchCopy.position; }; //if the swipe is detected, I update the real oldTouch if (stupidDetector.DetectSwipe(ref oldTouch, ref newTouch, direction, doSwipe + doUpdateRefTouch + doUpdateLocalTouch, distance)) { oldTouch = oldTouchCopy; return(true); } } } return(false); }
/// <summary>It detect the swipe only when the finger is lifted from the screen. /// It detect swipe if the distance between position is big enough or if the finger moves /// faster then the speed limit ///</summary> public override bool DetectSwipe(ref Touch oldTouch, ref Touch newTouch, SwipeDirection direction, OnSwipe doSwipe, float distance = -1) { deltaTime += newTouch.deltaTime; if (IsHorizontalSwipe(direction)) { deltaPosition += Math.Abs(newTouch.deltaPosition.x); } else if (IsHorizontalSwipe(direction)) { deltaPosition += Math.Abs(newTouch.deltaPosition.y); } if (isTouchGood(oldTouch, newTouch)) { if (oldTouch.position != newTouch.position) { StupidSwipeDetector stupidDetector = new StupidSwipeDetector(); //if the finger go in the opposite direction, i reset the delta time stupidDetector.DetectSwipe(ref oldTouch, ref newTouch, (SwipeDirection)(-(int)direction), resetDeltaTime, 1f); OnSwipe swipeAction = doSwipe + resetDeltaTime + resetDeltaPos; if (deltaTime / deltaPosition >= speedLimit) { Debug.Log("aaa"); return(stupidDetector.DetectSwipe(ref oldTouch, ref newTouch, direction, swipeAction, minDistSpeedDetection)); } else { Debug.Log("bbb"); return(stupidDetector.DetectSwipe(ref oldTouch, ref newTouch, direction, swipeAction, distance)); } } } return(false); }