70 lines
1.9 KiB
C#
70 lines
1.9 KiB
C#
using DG.Tweening;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class SlideScrollView : MonoBehaviour, IBeginDragHandler, IEndDragHandler
|
|
{
|
|
private RectTransform contentTrans;
|
|
private float beginMousePositionX;
|
|
private float endMousePositionX;
|
|
private ScrollRect scrollRect;
|
|
|
|
public int cellLength;
|
|
public int spaceing;
|
|
public int leftOffset;
|
|
|
|
private float moveOneItemLength;
|
|
private Vector3 currentContentLocalPos;//上一次的位置
|
|
|
|
public int totalItemNum;
|
|
private int currenIndex;
|
|
private void Awake()
|
|
{
|
|
scrollRect = GetComponent<ScrollRect>();
|
|
contentTrans = scrollRect.content;
|
|
moveOneItemLength = cellLength + spaceing;
|
|
currentContentLocalPos = contentTrans.localPosition;
|
|
currenIndex = 1;
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
endMousePositionX = Input.mousePosition.x;
|
|
float offSetX = 0;
|
|
float moveDistance = 0;//当次需要滑动的距离
|
|
offSetX = beginMousePositionX - endMousePositionX;
|
|
|
|
|
|
if (offSetX > 0)//右滑
|
|
{
|
|
if (currenIndex >= totalItemNum)
|
|
{
|
|
return;
|
|
}
|
|
moveDistance = -moveOneItemLength;
|
|
currenIndex++;
|
|
}
|
|
else//左滑
|
|
{
|
|
if (currenIndex <= 1)
|
|
{
|
|
return;
|
|
}
|
|
moveDistance = moveOneItemLength;
|
|
currenIndex--;
|
|
}
|
|
|
|
DOTween.To(() => contentTrans.localPosition, lerpValue => contentTrans.localPosition = lerpValue, currentContentLocalPos + new Vector3(moveDistance, 0, 0), 0.5f).SetEase(Ease.InOutQuint);
|
|
currentContentLocalPos += new Vector3(moveDistance, 0, 0);
|
|
}
|
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
beginMousePositionX = Input.mousePosition.x;
|
|
}
|
|
}
|
|
|