using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class PageView1 : MonoBehaviour/*, IBeginDragHandler, IEndDragHandler*/
{
private ScrollRect rect; //滑动组件
private float targethorizontal = 0; //滑动的起始坐标
private bool isDrag = false; //是否拖拽结束
private List<float> posList = new List<float>(); //求出每页的临界角,页索引从0开始
private int currentPageIndex = -1;
public Action<int> OnPageChanged;
public RectTransform content;
private bool stopMove = true;
public float smooting = 4; //滑动速度
public float sensitivity = 0;
private float startTime;
private float startDragHorizontal;
public float miniOffsetHorizontalNormalizedPosition = 15f;
private float mousePointX = 0;
private bool isDown = false;
public int currenIndex;
public int CurIndex
{
get
{
return currenIndex;
}
set
{
currenIndex = value;
}
}
// Start is called before the first frame update
void Start()
{
miniOffsetHorizontalNormalizedPosition = 15;
rect = transform.GetComponent<ScrollRect>();
var _rectWidth = GetComponent<RectTransform>();
var tempWidth = ((float)content.transform.childCount * _rectWidth.rect.width);
content.sizeDelta = new Vector2(tempWidth, _rectWidth.rect.height);
//未显示的长度
float horizontalLength = content.rect.width - _rectWidth.rect.width;
for (int i = 0; i < rect.content.transform.childCount; i++)
{
posList.Add(_rectWidth.rect.width * i / horizontalLength);
}
StartCoroutine(UpdateTime());
}
// Update is called once per frame
void Update()
{
if (!isDrag && !stopMove)
{
startTime += Time.deltaTime;
float t = startTime * smooting;
rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, targethorizontal, t);
if (t >= 1)
stopMove = true;
}
#region 滑动
if (Input.GetMouseButtonDown(0))
{
mousePointX = Input.mousePosition.x;
isDown = true;
}
if (Input.GetMouseButtonUp(0))
{
if (isDown)
{
Debug.Log("NX:" + Input.mousePosition.x + " OX:" + mousePointX);
if (mousePointX > (Input.mousePosition.x + miniOffsetHorizontalNormalizedPosition))
{
Debug.Log("下一页");
//CurIndex += 1;
PageDown();
CountDownNumber = 5;
}
else if (mousePointX < (Input.mousePosition.x - miniOffsetHorizontalNormalizedPosition))
{
Debug.Log("上一页");
PageUp();
//CurIndex -= 1;
CountDownNumber = 5;
}
}
targethorizontal = posList[CurIndex]; //设置当前坐标,更新函数进行插值
//isDrag = false;
startTime = 0;
isDown = false;
}
#endregion
}
public void pageTo(int index)
{
if (index >= 0 && index < posList.Count)
{
rect.horizontalNormalizedPosition = posList[index];
SetPageIndex(index);
currenIndex = index;
}
CountDownNumber = 5;
}
/*
public void OnBeginDrag(PointerEventData eventData)
{
isDrag = true;
//开始拖动
startDragHorizontal = rect.horizontalNormalizedPosition;
}
public void OnEndDrag(PointerEventData eventData)
{
float posX = rect.horizontalNormalizedPosition;
posX += ((posX - startDragHorizontal) * sensitivity);
posX = posX < 1 ? posX : 1;
posX = posX > 0 ? posX : 0;
int index = 0;
float offset = Mathf.Abs(posList[index] - posX);
//Debug.Log("offset " + offset);
for (int i = 1; i < posList.Count; i++)
{
float temp = Mathf.Abs(posList[i] - posX);
if (temp < offset)
{
index = i;
offset = temp;
}
}
SetPageIndex(index);
targethorizontal = posList[index]; //设置当前坐标,更新函数进行插值
isDrag = false;
startTime = 0;
stopMove = false;
}
/*/
private void SetPageIndex(int index)
{
if (currentPageIndex != index)
{
currentPageIndex = index;
if (OnPageChanged != null)
OnPageChanged(index);
}
}
/// <summary>
/// 上一页
/// </summary>
public void PageUp()
{
if (posList.Count == 0)
{
return;
}
int pageIndex = CurIndex - 1;
if (pageIndex < 0)
{
//CurIndex = 0;
CurIndex = posList.Count - 1;
}
//else if (pageIndex >= posList.Count - 1)
//{
// //CurIndex = posList.Count - 1;
// CurIndex = 0;
//}
else
{
CurIndex = pageIndex;
}
Debug.Log(currenIndex);
pageTo(currenIndex);
//toggleArray[CurIndex].isOn = true;
}
/// <summary>
/// 下一页
/// </summary>
public void PageDown()
{
if (posList.Count == 0)
{
return;
}
int pageIndex = CurIndex + 1;
//if (pageIndex < 0)
//{
// //CurIndex = 0;
// CurIndex = posList.Count - 1;
//}
if (pageIndex > posList.Count - 1)
{
//CurIndex = posList.Count - 1;
CurIndex = 0;
}
else
{
CurIndex = pageIndex;
}
//Debug.Log(currenIndex);
isDrag = true;
pageTo(currenIndex);
//toggleArray[CurIndex].isOn = true;
}
public float CountDownNumber;
public IEnumerator UpdateTime()
{
CountDownNumber = 5;
while (CountDownNumber > 0)
{
yield return new WaitForSeconds(1);
//Debug.Log(countDownNumber);
CountDownNumber--;
//int index = 0;
if (CountDownNumber == 0)
{
PageDown();
}
}
}
}
unity 单页滑动
最新推荐文章于 2024-09-24 16:38:07 发布