Merged in fix/ScrollSnapFix (pull request #74)

Resolution for 323
pull/413/head
Simon Jackson 2020-06-22 15:01:54 +00:00
commit 93fda056a0
3 changed files with 33 additions and 28 deletions

View File

@ -27,7 +27,7 @@ namespace UnityEngine.UI.Extensions
{
if (!_settled && !_pointerDown)
{
if (!IsRectSettledOnaPage(_screensContainer.localPosition))
if (!IsRectSettledOnaPage(_screensContainer.anchoredPosition))
{
ScrollToClosestElement();
}
@ -36,16 +36,16 @@ namespace UnityEngine.UI.Extensions
}
else if (_lerp)
{
_screensContainer.localPosition = Vector3.Lerp(_screensContainer.localPosition, _lerp_target, transitionSpeed * (UseTimeScale ? Time.deltaTime : Time.unscaledDeltaTime));
if (Vector3.Distance(_screensContainer.localPosition, _lerp_target) < 0.1f)
_screensContainer.anchoredPosition = Vector3.Lerp(_screensContainer.anchoredPosition, _lerp_target, transitionSpeed * (UseTimeScale ? Time.deltaTime : Time.unscaledDeltaTime));
if (Vector3.Distance(_screensContainer.anchoredPosition, _lerp_target) < 0.1f)
{
_screensContainer.localPosition = _lerp_target;
_screensContainer.anchoredPosition = _lerp_target;
_lerp = false;
EndScreenChange();
}
}
CurrentPage = GetPageforPosition(_screensContainer.localPosition);
CurrentPage = GetPageforPosition(_screensContainer.anchoredPosition);
//If the container is moving check if it needs to settle on a page
if (!_pointerDown)
@ -196,7 +196,7 @@ namespace UnityEngine.UI.Extensions
private void SetScrollContainerPosition()
{
_scrollStartPosition = _screensContainer.localPosition.x;
_scrollStartPosition = _screensContainer.anchoredPosition.x;
_scroll_rect.horizontalNormalizedPosition = (float)(_currentPage) / (_screens - 1);
OnCurrentScreenChange(_currentPage);
}
@ -251,7 +251,7 @@ namespace UnityEngine.UI.Extensions
}
else
{
var distance = Vector3.Distance(_startPosition, _screensContainer.localPosition);
var distance = Vector3.Distance(_startPosition, _screensContainer.anchoredPosition);
if (UseHardSwipe)
{
@ -259,7 +259,7 @@ namespace UnityEngine.UI.Extensions
if (distance > FastSwipeThreshold)
{
if (_startPosition.x - _screensContainer.localPosition.x > 0)
if (_startPosition.x - _screensContainer.anchoredPosition.x > 0)
{
NextScreen();
}
@ -278,9 +278,9 @@ namespace UnityEngine.UI.Extensions
if (UseFastSwipe && distance < panelDimensions.width && distance >= FastSwipeThreshold)
{
_scroll_rect.velocity = Vector3.zero;
if (_startPosition.x - _screensContainer.localPosition.x > 0)
if (_startPosition.x - _screensContainer.anchoredPosition.x > 0)
{
if (_startPosition.x - _screensContainer.localPosition.x > _childSize / 3)
if (_startPosition.x - _screensContainer.anchoredPosition.x > _childSize / 3)
{
ScrollToClosestElement();
}
@ -291,7 +291,7 @@ namespace UnityEngine.UI.Extensions
}
else
{
if (_startPosition.x - _screensContainer.localPosition.x < -_childSize / 3)
if (_startPosition.x - _screensContainer.anchoredPosition.x < -_childSize / 3)
{
ScrollToClosestElement();
}

View File

@ -8,7 +8,7 @@ using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions
{
public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IScrollSnap
public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IScrollSnap, IPointerClickHandler
{
internal Rect panelDimensions;
internal RectTransform _screensContainer;
@ -390,13 +390,13 @@ namespace UnityEngine.UI.Extensions
_childPos = -_childSize * page;
if (_isVertical)
{
_infiniteOffset = _screensContainer.localPosition.y < 0 ? -_screensContainer.sizeDelta.y * _infiniteWindow : _screensContainer.sizeDelta.y * _infiniteWindow;
_infiniteOffset = _screensContainer.anchoredPosition.y < 0 ? -_screensContainer.sizeDelta.y * _infiniteWindow : _screensContainer.sizeDelta.y * _infiniteWindow;
_infiniteOffset = _infiniteOffset == 0 ? 0 : _infiniteOffset < 0 ? _infiniteOffset - _childSize * _infiniteWindow : _infiniteOffset + _childSize * _infiniteWindow;
target.y = _childPos + _scrollStartPosition + _infiniteOffset;
}
else
{
_infiniteOffset = _screensContainer.localPosition.x < 0 ? -_screensContainer.sizeDelta.x * _infiniteWindow : _screensContainer.sizeDelta.x * _infiniteWindow;
_infiniteOffset = _screensContainer.anchoredPosition.x < 0 ? -_screensContainer.sizeDelta.x * _infiniteWindow : _screensContainer.sizeDelta.x * _infiniteWindow;
_infiniteOffset = _infiniteOffset == 0 ? 0 : _infiniteOffset < 0 ? _infiniteOffset - _childSize * _infiniteWindow : _infiniteOffset + _childSize * _infiniteWindow;
target.x = _childPos + _scrollStartPosition + _infiniteOffset;
}
@ -408,7 +408,7 @@ namespace UnityEngine.UI.Extensions
internal void ScrollToClosestElement()
{
_lerp = true;
CurrentPage = GetPageforPosition(_screensContainer.localPosition);
CurrentPage = GetPageforPosition(_screensContainer.anchoredPosition);
GetPositionforPage(_currentPage, ref _lerp_target);
OnCurrentScreenChange(_currentPage);
}
@ -570,7 +570,7 @@ namespace UnityEngine.UI.Extensions
_pointerDown = true;
_settled = false;
StartScreenChange();
_startPosition = _screensContainer.localPosition;
_startPosition = _screensContainer.anchoredPosition;
}
/// <summary>
@ -593,7 +593,7 @@ namespace UnityEngine.UI.Extensions
/// </summary>
int IScrollSnap.CurrentPage()
{
return CurrentPage = GetPageforPosition(_screensContainer.localPosition);
return CurrentPage = GetPageforPosition(_screensContainer.anchoredPosition);
}
/// <summary>
@ -612,6 +612,11 @@ namespace UnityEngine.UI.Extensions
GoToScreen(page);
}
public void OnPointerClick(PointerEventData eventData)
{
var position = _screensContainer.anchoredPosition;
}
#endregion
}
}

View File

@ -27,7 +27,7 @@ namespace UnityEngine.UI.Extensions
{
if (!_settled && !_pointerDown)
{
if (!IsRectSettledOnaPage(_screensContainer.localPosition))
if (!IsRectSettledOnaPage(_screensContainer.anchoredPosition))
{
ScrollToClosestElement();
}
@ -36,16 +36,16 @@ namespace UnityEngine.UI.Extensions
}
else if (_lerp)
{
_screensContainer.localPosition = Vector3.Lerp(_screensContainer.localPosition, _lerp_target, transitionSpeed * (UseTimeScale ? Time.deltaTime : Time.unscaledDeltaTime));
if (Vector3.Distance(_screensContainer.localPosition, _lerp_target) < 0.1f)
_screensContainer.anchoredPosition = Vector3.Lerp(_screensContainer.anchoredPosition, _lerp_target, transitionSpeed * (UseTimeScale ? Time.deltaTime : Time.unscaledDeltaTime));
if (Vector3.Distance(_screensContainer.anchoredPosition, _lerp_target) < 0.1f)
{
_screensContainer.localPosition = _lerp_target;
_screensContainer.anchoredPosition = _lerp_target;
_lerp = false;
EndScreenChange();
}
}
CurrentPage = GetPageforPosition(_screensContainer.localPosition);
CurrentPage = GetPageforPosition(_screensContainer.anchoredPosition);
//If the container is moving check if it needs to settle on a page
if (!_pointerDown)
@ -193,7 +193,7 @@ namespace UnityEngine.UI.Extensions
private void SetScrollContainerPosition()
{
_scrollStartPosition = _screensContainer.localPosition.y;
_scrollStartPosition = _screensContainer.anchoredPosition.y;
_scroll_rect.verticalNormalizedPosition = (float)(_currentPage) / (_screens - 1);
OnCurrentScreenChange(_currentPage);
}
@ -244,14 +244,14 @@ namespace UnityEngine.UI.Extensions
}
else
{
var distance = Vector3.Distance(_startPosition, _screensContainer.localPosition);
var distance = Vector3.Distance(_startPosition, _screensContainer.anchoredPosition);
if (UseHardSwipe)
{
_scroll_rect.velocity = Vector3.zero;
if (distance > FastSwipeThreshold)
{
if (_startPosition.y - _screensContainer.localPosition.y > 0)
if (_startPosition.y - _screensContainer.anchoredPosition.y > 0)
{
NextScreen();
}
@ -270,9 +270,9 @@ namespace UnityEngine.UI.Extensions
if (UseFastSwipe && distance < panelDimensions.height + FastSwipeThreshold && distance >= 1f)
{
_scroll_rect.velocity = Vector3.zero;
if (_startPosition.y - _screensContainer.localPosition.y > 0)
if (_startPosition.y - _screensContainer.anchoredPosition.y > 0)
{
if (_startPosition.y - _screensContainer.localPosition.y > _childSize / 3)
if (_startPosition.y - _screensContainer.anchoredPosition.y > _childSize / 3)
{
ScrollToClosestElement();
}
@ -283,7 +283,7 @@ namespace UnityEngine.UI.Extensions
}
else
{
if (_startPosition.y - _screensContainer.localPosition.y > -_childSize / 3)
if (_startPosition.y - _screensContainer.anchoredPosition.y > -_childSize / 3)
{
ScrollToClosestElement();
}