Unity之UGUI翻页效果

Unity之UGUI翻页效果_第1张图片

 

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 posList = new List();//存位置
    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 toggleArray = new List();

   // public CallBack callbackPrevDrag;
  ///  public CallBack callbackNextDrag;

    //重新初始化一下
    public void initialize()
    {
        posList.Clear();
        float horizontalLength = rect.content.rect.width - GetComponent().rect.width;
        var _rectWidth = GetComponent().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());
        }
        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();
        toggleGroup = transform.Find("toggleGroup").GetComponent();
        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

你可能感兴趣的:(Unity3D)