Updated FastSwipe logic to measure the distance of a swipe against the container width (with threshold offset) rather than using velocity (which was messy)

Resolves #130
release
Simon Jackson 2017-07-02 14:22:10 +01:00
parent 49ecdc1b0e
commit 439242d914
4 changed files with 21 additions and 36 deletions

View File

@ -311,12 +311,12 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
StartingScreen: 0 StartingScreen: 0
PageStep: 2 PageStep: 2.7
Pagination: {fileID: 266467291} Pagination: {fileID: 266467291}
PrevButton: {fileID: 1112321044} PrevButton: {fileID: 1112321044}
NextButton: {fileID: 803284189} NextButton: {fileID: 803284189}
transitionSpeed: 7.5 transitionSpeed: 7.5
UseFastSwipe: 0 UseFastSwipe: 1
FastSwipeThreshold: 100 FastSwipeThreshold: 100
SwipeVelocityThreshold: 200 SwipeVelocityThreshold: 200
MaskArea: {fileID: 0} MaskArea: {fileID: 0}
@ -1892,7 +1892,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 1, y: 1} m_AnchorMin: {x: 1, y: 1}
m_AnchorMax: {x: 1, y: 1} m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: -113.9, y: -183.2} m_AnchoredPosition: {x: -113.899994, y: -183.2}
m_SizeDelta: {x: 150, y: 30} m_SizeDelta: {x: 150, y: 30}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &803284191 --- !u!114 &803284191

View File

@ -72,7 +72,6 @@ namespace UnityEngine.UI.Extensions
int _offset = 0; int _offset = 0;
float _dimension = 0; float _dimension = 0;
Rect panelDimensions = gameObject.GetComponent<RectTransform>().rect;
float currentXPosition = 0; float currentXPosition = 0;
var pageStepValue = _childSize = (int)panelDimensions.width * ((PageStep == 0) ? 3 : PageStep); var pageStepValue = _childSize = (int)panelDimensions.width * ((PageStep == 0) ? 3 : PageStep);
@ -88,7 +87,7 @@ namespace UnityEngine.UI.Extensions
_dimension = currentXPosition + _offset * -1; _dimension = currentXPosition + _offset * -1;
_screensContainer.GetComponent<RectTransform>().offsetMax = new Vector2(_dimension, 0f); _screensContainer.offsetMax = new Vector2(_dimension, 0f);
} }
/// <summary> /// <summary>
@ -214,25 +213,17 @@ namespace UnityEngine.UI.Extensions
if (_scroll_rect.horizontal) if (_scroll_rect.horizontal)
{ {
if (UseFastSwipe) var distance = Vector3.Distance(_startPosition, _screensContainer.localPosition);
if (UseFastSwipe && distance < panelDimensions.width + FastSwipeThreshold)
{ {
//If using fastswipe - then a swipe does page next / previous _scroll_rect.velocity = Vector3.zero;
if ((_scroll_rect.velocity.x > 0 &&_scroll_rect.velocity.x > FastSwipeThreshold) || if (_startPosition.x - _screensContainer.localPosition.x > 0)
_scroll_rect.velocity.x < 0 && _scroll_rect.velocity.x < -FastSwipeThreshold)
{ {
_scroll_rect.velocity = Vector3.zero; NextScreen();
if (_startPosition.x - _screensContainer.localPosition.x > 0)
{
NextScreen();
}
else
{
PreviousScreen();
}
} }
else else
{ {
ScrollToClosestElement(); PreviousScreen();
} }
} }
} }

View File

@ -10,6 +10,7 @@ namespace UnityEngine.UI.Extensions
{ {
public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler, IScrollSnap public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler, IScrollSnap
{ {
internal Rect panelDimensions;
internal RectTransform _screensContainer; internal RectTransform _screensContainer;
internal bool _isVertical; internal bool _isVertical;
@ -29,8 +30,8 @@ namespace UnityEngine.UI.Extensions
internal int _currentPage; internal int _currentPage;
internal int _previousPage; internal int _previousPage;
internal int _halfNoVisibleItems; internal int _halfNoVisibleItems;
internal bool _moveStarted;
private int _bottomItem, _topItem; private int _bottomItem, _topItem;
private bool _moveStarted;
[Serializable] [Serializable]
public class SelectionChangeStartEvent : UnityEvent { } public class SelectionChangeStartEvent : UnityEvent { }
@ -63,7 +64,7 @@ namespace UnityEngine.UI.Extensions
[Tooltip("Fast Swipe makes swiping page next / previous (optional)")] [Tooltip("Fast Swipe makes swiping page next / previous (optional)")]
public Boolean UseFastSwipe = false; public Boolean UseFastSwipe = false;
[Tooltip("How far swipe has to travel to initiate a page change (optional)")] [Tooltip("Offset for how far a swipe has to travel to initiate a page change (optional)\nDefault is the panel dimensions")]
public int FastSwipeThreshold = 100; public int FastSwipeThreshold = 100;
[Tooltip("Speed at which the ScrollRect will keep scrolling before slowing down and stopping (optional)")] [Tooltip("Speed at which the ScrollRect will keep scrolling before slowing down and stopping (optional)")]
@ -139,7 +140,8 @@ namespace UnityEngine.UI.Extensions
var vscroll = _scroll_rect.verticalScrollbar.gameObject.AddComponent<ScrollSnapScrollbarHelper>(); var vscroll = _scroll_rect.verticalScrollbar.gameObject.AddComponent<ScrollSnapScrollbarHelper>();
vscroll.ss = this; vscroll.ss = this;
} }
panelDimensions = gameObject.GetComponent<RectTransform>().rect;
if (StartingScreen < 0) if (StartingScreen < 0)
{ {
StartingScreen = 0; StartingScreen = 0;

View File

@ -214,25 +214,17 @@ namespace UnityEngine.UI.Extensions
if (_scroll_rect.vertical) if (_scroll_rect.vertical)
{ {
if (UseFastSwipe) var distance = Vector3.Distance(_startPosition, _screensContainer.localPosition);
if (UseFastSwipe && distance < panelDimensions.height + FastSwipeThreshold)
{ {
//If using fastswipe - then a swipe does page next / previous _scroll_rect.velocity = Vector3.zero;
if ((_scroll_rect.velocity.y > 0 && _scroll_rect.velocity.y > FastSwipeThreshold) || if (_startPosition.y - _screensContainer.localPosition.y > 0)
_scroll_rect.velocity.y < 0 && _scroll_rect.velocity.y < -FastSwipeThreshold)
{ {
_scroll_rect.velocity = Vector3.zero; NextScreen();
if (_startPosition.y - _screensContainer.localPosition.y > 0)
{
NextScreen();
}
else
{
PreviousScreen();
}
} }
else else
{ {
ScrollToClosestElement(); PreviousScreen();
} }
} }
} }