This commit is contained in:
fatiao 2025-12-11 14:13:49 +08:00
commit 4006f4a56b

View File

@ -18,7 +18,6 @@ public class StepScrollRect : MonoBehaviour, IBeginDragHandler, IDragHandler, IE
public Axis axis;
ScrollRect sr;
LoopListView loopview;
RectTransform content_rect;
bool isDragging;
private Vector2 dragStartPos;
@ -26,19 +25,13 @@ public class StepScrollRect : MonoBehaviour, IBeginDragHandler, IDragHandler, IE
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);
loopview.mOnEndDragAction = () => loopview_OnEndDrag(loopview, axis, dragEndPos - dragStartPos, dragthreshold,curpos);
}
@ -66,41 +59,6 @@ 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范围内
@ -120,15 +78,15 @@ public class StepScrollRect : MonoBehaviour, IBeginDragHandler, IDragHandler, IE
void loopview_OnEndDrag(LoopListView LoopView, Axis axis, Vector2 draglenth, float dragthreshold = 50)
void loopview_OnEndDrag(LoopListView LoopView, Axis axis, Vector2 draglenth, float dragthreshold,int curpos)
{
Debug.Log("loopview_OnEndDrag");
Debug.Log("axis:" + axis + " draglenth:" + draglenth + " dragthreshold:" + dragthreshold);
#region loopview的snap选项框
// 获取当前最接近视口中心的项目索引
//int curNearestItemIndex = LoopView.CurSnapNearestItemIndex;
int curNearestItemIndex = curpos;
//int curNearestItemIndex= LoopView.CurSnapNearestItemIndex;
LoopListViewItem item = LoopView.GetShownItemByItemIndex(curNearestItemIndex);
Debug.Log("curNearestItemIndex:" + (item != null ? item.name : "null"));
if (item == null)
@ -140,7 +98,6 @@ public class StepScrollRect : MonoBehaviour, IBeginDragHandler, IDragHandler, IE
if (dragthreshold < 50) dragthreshold = 50;
int nextpos = curNearestItemIndex;
int dir = 1;
@ -163,8 +120,8 @@ 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);
curpos = nextpos;
}
}