using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
public class PageView : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
public ScrollRect rect;
private float targethorizontal = 0;
private List<float> posList = new List<float>();//存位置
private bool isDrag = true;
private float startTime = 0;
private float startDragHorizontal;
private int curIndex = 0;
public float speed = 4; //滑動速度
public float sensitivity = 0;
public ToggleGroup toggleGroup;
List<Toggle> toggleArray = new List<Toggle>();
// public CallBack<int> callbackPrevDrag;
/// public CallBack<int> callbackNextDrag;
//重新初始化一下
public void initialize()
{
posList.Clear();
float horizontalLength = rect.content.rect.width - GetComponent<RectTransform>().rect.width;
var _rectWidth = GetComponent<RectTransform>().rect.width;
for (int i = 0; i < rect.content.transform.childCount; i++)
{
posList.Add(_rectWidth * i / horizontalLength);
}
curIndex = 0;
for (int i = 0; i < toggleGroup.transform.childCount; ++i)
{
toggleArray.Add(toggleGroup.transform.GetChild(i).GetComponent<Toggle>());
}
if (toggleArray.Count > 0)
{
for (int i = 0; i < toggleArray.Count; i++)
{
toggleArray[i].isOn = false;
}
toggleArray[0].isOn = true;
}
}
public void destroy()
{
isDrag = true;
startTime = 0;
curIndex = 0;
speed = 4;
sensitivity = 0;
posList.Clear();
toggleArray.Clear();
}
void Awake()
{
rect = GetComponent<ScrollRect>();
toggleGroup = transform.Find("toggleGroup").GetComponent<ToggleGroup>();
initialize();
}
void Update()
{
if (!isDrag)
{
startTime += Time.deltaTime;
float t = startTime * speed;
rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, targethorizontal, t); //加速滑動效果
//rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, targethorizontal, Time.deltaTime * speed); //緩慢勻速滑動效果
}
}
public void OnBeginDrag(PointerEventData eventData)
{
//Debug.Log("OnBeginDrag");
isDrag = true;
//開始拖動
startDragHorizontal = rect.horizontalNormalizedPosition;
//Debug.Log("start startDragHorizontal:" + startDragHorizontal);
}
public void OnEndDrag(PointerEventData eventData)
{
//Debug.Log("OnEndDrag");
float posX = rect.horizontalNormalizedPosition;
//Debug.Log("end posX:" + posX);
/*
int index = 0;
float offset = Mathf.Abs(posList[index] - posX); //計算當前位置與第一頁的偏移量
for (int i = 1; i < posList.Count; i++)
{ //遍歷頁籤,選取偏移量最小的那個頁面
float temp = Mathf.Abs(posList[i] - posX);
if (temp < offset)
{
index = i;
offset = temp;
}
}
curIndex = index;*/
if (Mathf.Abs(startDragHorizontal - posX) >= 0.02f)
{
if (startDragHorizontal - posX > 0)
{
curIndex--;
if (curIndex < 0)
{
curIndex = 0;
}
// callbackPrevDrag?.Invoke(curIndex);
}
else
{
curIndex++;
if (curIndex >= posList.Count)
{
curIndex = posList.Count - 1;
}
// callbackNextDrag?.Invoke(curIndex);
}
}
targethorizontal = posList[curIndex]; //設置當前座標,更新函數進行插值
isDrag = false;
startTime = 0;
if (toggleArray.Count > 0)
{
toggleArray[curIndex].isOn = true;
}
//Debug.Log("OnEndDrag:" + curIndex);
}
public void pageTo(int index)
{
Debug.Log("pageTo......");
curIndex = index;
targethorizontal = posList[curIndex]; //設置當前座標,更新函數進行插值
isDrag = false;
startTime = 0;
toggleArray[curIndex].isOn = true;
// callbackNextDrag?.Invoke(curIndex);
}
}
上次沒有翻頁Tooge效果,更新一下https://blog.csdn.net/weixin_41814169/article/details/88890650