This commit is contained in:
fatiao 2025-12-11 11:15:18 +08:00
commit 5df668b28c

View File

@ -18,18 +18,27 @@ public class StepScrollRect : MonoBehaviour, IBeginDragHandler, IDragHandler, IE
public Axis axis;
ScrollRect sr;
LoopListView loopview;
RectTransform content_rect;
bool isDragging;
private Vector2 dragStartPos;
private Vector2 dragEndPos;
public float dragthreshold = 50;
public float dragthreshold = 200;
public int curpos;
public LoopListViewItem curitem { get { return loopview.GetShownItemByItemIndex(curpos); } }
void Start()
{
sr = GetComponent<ScrollRect>();
loopview = GetComponent<LoopListView>();
content_rect = sr.content.GetComponent<RectTransform>();
SetAxis();
loopview.mOnEndDragAction = () => loopview_OnEndDrag(loopview, axis, dragEndPos - dragStartPos, dragthreshold);
}
@ -38,6 +47,7 @@ public class StepScrollRect : MonoBehaviour, IBeginDragHandler, IDragHandler, IE
Debug.Log("OnBeginDrag");
isDragging = true;
dragStartPos = eventData.position;
curpos = loopview.CurSnapNearestItemIndex;
}
public void OnEndDrag(PointerEventData eventData)
@ -56,6 +66,41 @@ public class StepScrollRect : MonoBehaviour, IBeginDragHandler, IDragHandler, IE
dragEndPos = eventData.position;
}
void SetAxis()
{
switch (axis)
{
case Axis.left_2_right:
loopview.ArrangeType = ListItemArrangeType.LeftToRight;
sr.horizontal = true;
sr.vertical = false;
content_rect.anchorMin = new Vector2(0.5f, 0);
content_rect.anchorMax = new Vector2(0.5f, 1);
break;
case Axis.right_2_left:
loopview.ArrangeType = ListItemArrangeType.RightToLeft;
sr.horizontal = true;
sr.vertical = false;
content_rect.anchorMin = new Vector2(0.5f, 0);
content_rect.anchorMax = new Vector2(0.5f, 1);
break;
case Axis.top_2_bottom:
loopview.ArrangeType = ListItemArrangeType.TopToBottom;
sr.horizontal = false;
sr.vertical = true;
content_rect.anchorMin = new Vector2(0, 0.5f);
content_rect.anchorMax = new Vector2(1, 0.5f);
break;
case Axis.bottom_2_top:
loopview.ArrangeType = ListItemArrangeType.BottomToTop;
sr.horizontal = false;
sr.vertical = true;
content_rect.anchorMin = new Vector2(0, 0.5f);
content_rect.anchorMax = new Vector2(1, 0.5f);
break;
}
}
void AreaControl(PointerEventData eventData)
{
// 检查拖动点是否在ScrollRect范围内
@ -82,7 +127,8 @@ public class StepScrollRect : MonoBehaviour, IBeginDragHandler, IDragHandler, IE
#region loopview的snap选项框
// 获取当前最接近视口中心的项目索引
int curNearestItemIndex = LoopView.CurSnapNearestItemIndex;
//int curNearestItemIndex = LoopView.CurSnapNearestItemIndex;
int curNearestItemIndex = curpos;
LoopListViewItem item = LoopView.GetShownItemByItemIndex(curNearestItemIndex);
Debug.Log("curNearestItemIndex:" + (item != null ? item.name : "null"));
if (item == null)
@ -94,6 +140,7 @@ public class StepScrollRect : MonoBehaviour, IBeginDragHandler, IDragHandler, IE
if (dragthreshold < 50) dragthreshold = 50;
int nextpos = curNearestItemIndex;
int dir = 1;
@ -104,8 +151,8 @@ public class StepScrollRect : MonoBehaviour, IBeginDragHandler, IDragHandler, IE
if (axis == Axis.right_2_left) dir = -1;
//从左往右划
if (draglenth.x > dragthreshold) nextpos = curNearestItemIndex + dir;
else if (draglenth.x < -dragthreshold) nextpos = curNearestItemIndex - dir;
if (draglenth.x > dragthreshold) nextpos = curNearestItemIndex - dir;
else if (draglenth.x < -dragthreshold) nextpos = curNearestItemIndex + dir;
}
if (axis == Axis.top_2_bottom || axis == Axis.bottom_2_top) // 垂直
@ -116,6 +163,7 @@ public class StepScrollRect : MonoBehaviour, IBeginDragHandler, IDragHandler, IE
if (draglenth.y > dragthreshold) nextpos = curNearestItemIndex + dir;
else if (draglenth.y < -dragthreshold) nextpos = curNearestItemIndex - dir;
}
curpos = nextpos;
loopview.SetSnapTargetItemIndex(nextpos);
}
}