Added optional event delta to OnEndDrag - to allow bail out of swipe action if swipe motion is actually negligible at end of drag

pull/413/head
Martin Shuttleworth 2019-07-10 17:36:36 +01:00
parent 1dc2b604e2
commit 61e019ac26
3 changed files with 91 additions and 68 deletions

View File

@ -2,6 +2,7 @@
/// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-1945602
/// Updated by ddreaper - removed dependency on a custom ScrollRect script. Now implements drag interfaces and standard Scroll Rect.
using System;
using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions
@ -246,55 +247,62 @@ namespace UnityEngine.UI.Extensions
if (_scroll_rect.horizontal)
{
var distance = Vector3.Distance(_startPosition, _screensContainer.localPosition);
if (UseHardSwipe)
if (UseSwipeDeltaThreshold && Math.Abs(eventData.delta.x) < SwipeDeltaThreshold)
{
_scroll_rect.velocity = Vector3.zero;
if (distance > FastSwipeThreshold)
{
if (_startPosition.x - _screensContainer.localPosition.x > 0)
{
NextScreen();
}
else
{
PreviousScreen();
}
}
else
{
ScrollToClosestElement();
}
ScrollToClosestElement();
}
else
{
if (UseFastSwipe && distance < panelDimensions.width && distance >= FastSwipeThreshold)
var distance = Vector3.Distance(_startPosition, _screensContainer.localPosition);
if (UseHardSwipe)
{
_scroll_rect.velocity = Vector3.zero;
if (_startPosition.x - _screensContainer.localPosition.x > 0)
if (distance > FastSwipeThreshold)
{
if (_startPosition.x - _screensContainer.localPosition.x > _childSize / 3)
{
ScrollToClosestElement();
}
else
if (_startPosition.x - _screensContainer.localPosition.x > 0)
{
NextScreen();
}
}
else
{
if (_startPosition.x - _screensContainer.localPosition.x < -_childSize / 3)
{
ScrollToClosestElement();
}
else
{
PreviousScreen();
}
}
else
{
ScrollToClosestElement();
}
}
else
{
if (UseFastSwipe && distance < panelDimensions.width && distance >= FastSwipeThreshold)
{
_scroll_rect.velocity = Vector3.zero;
if (_startPosition.x - _screensContainer.localPosition.x > 0)
{
if (_startPosition.x - _screensContainer.localPosition.x > _childSize / 3)
{
ScrollToClosestElement();
}
else
{
NextScreen();
}
}
else
{
if (_startPosition.x - _screensContainer.localPosition.x < -_childSize / 3)
{
ScrollToClosestElement();
}
else
{
PreviousScreen();
}
}
}
}
}
}

View File

@ -69,12 +69,18 @@ namespace UnityEngine.UI.Extensions
[Tooltip("Fast Swipe makes swiping page next / previous (optional)")]
public Boolean UseFastSwipe = false;
[Tooltip("Swipe Delta Threshold looks at the speed of input to decide if a swipe will be initiated (optional)")]
public Boolean UseSwipeDeltaThreshold = false;
[Tooltip("Offset for how far a swipe has to travel to initiate a page change (optional)")]
public int FastSwipeThreshold = 100;
[Tooltip("Speed at which the ScrollRect will keep scrolling before slowing down and stopping (optional)")]
public int SwipeVelocityThreshold = 100;
[Tooltip("Threshold for swipe speed to initiate a swipe, below threshold will return to closest page (optional)")]
public float SwipeDeltaThreshold = 5.0f;
[Tooltip("Use time scale instead of unscaled time (optional)")]
public Boolean UseTimeScale = true;

View File

@ -3,6 +3,7 @@
/// Updated by SimonDarksideJ - removed dependency on a custom ScrollRect script. Now implements drag interfaces and standard Scroll Rect.
/// Updated by SimonDarksideJ - major refactoring on updating current position and scroll management
using System;
using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions
@ -238,54 +239,62 @@ namespace UnityEngine.UI.Extensions
if (_scroll_rect.vertical)
{
var distance = Vector3.Distance(_startPosition, _screensContainer.localPosition);
if(UseHardSwipe){
_scroll_rect.velocity = Vector3.zero;
if (distance > FastSwipeThreshold)
{
if (_startPosition.y - _screensContainer.localPosition.y > 0)
{
NextScreen();
}
else
{
PreviousScreen();
}
}
else
{
ScrollToClosestElement();
}
if (UseSwipeDeltaThreshold && Math.Abs(eventData.delta.y) < SwipeDeltaThreshold)
{
ScrollToClosestElement();
}
else
{
if (UseFastSwipe && distance < panelDimensions.height + FastSwipeThreshold && distance >=1f)
var distance = Vector3.Distance(_startPosition, _screensContainer.localPosition);
if (UseHardSwipe)
{
_scroll_rect.velocity = Vector3.zero;
if (_startPosition.y - _screensContainer.localPosition.y > 0)
if (distance > FastSwipeThreshold)
{
if (_startPosition.y - _screensContainer.localPosition.y > _childSize / 3)
{
ScrollToClosestElement();
}
else
if (_startPosition.y - _screensContainer.localPosition.y > 0)
{
NextScreen();
}
}
else
{
if (_startPosition.y - _screensContainer.localPosition.y > -_childSize / 3)
{
ScrollToClosestElement();
}
else
{
PreviousScreen();
}
}
}
else
{
ScrollToClosestElement();
}
}
else
{
if (UseFastSwipe && distance < panelDimensions.height + FastSwipeThreshold && distance >= 1f)
{
_scroll_rect.velocity = Vector3.zero;
if (_startPosition.y - _screensContainer.localPosition.y > 0)
{
if (_startPosition.y - _screensContainer.localPosition.y > _childSize / 3)
{
ScrollToClosestElement();
}
else
{
NextScreen();
}
}
else
{
if (_startPosition.y - _screensContainer.localPosition.y > -_childSize / 3)
{
ScrollToClosestElement();
}
else
{
PreviousScreen();
}
}
}
}
}
}
}