Unity3D PageView的效果 -CSDN

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using System;

public class PageView : 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;                  //當前所在的index
    public Action<int> OnPageChanged;                   //page改變的回調函數

    private bool stopMove = true;
    public float smooting = 4;                          //滑動速度  
    public float sensitivity = 0;                       //翻頁的靈敏度
    private float startTime;

    private float startDragHorizontal;                  //開始拖拽的Horizontal


    //-----------------------------------------------------------------------------------
    public void pageTo(int index)
    {
        if (index >= 0 && index < posList.Count)
        {
            rect.horizontalNormalizedPosition = posList[index];
            SetPageIndex(index);
        }
        else
        {
            Debug.LogWarning("頁碼不存在");
        }
    }


    //-----------------------------------------------------------------------------------
    void Awake () {
        rect = transform.GetComponent<ScrollRect> ();
        float horizontalLength = rect.content.rect.width - GetComponent<RectTransform> ().rect.width;
        posList.Add (0);
        for(int i = 1; i < rect.content.transform.childCount - 1; i++) {
            posList.Add (GetComponent<RectTransform> ().rect.width * i / horizontalLength);
        }
        posList.Add (1);
    }

    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;
        }
    }

    private void SetPageIndex (int index) {
        if(currentPageIndex != index) {
            currentPageIndex = index;
            if(OnPageChanged != null)
                OnPageChanged (index);
        }
    }

    public void OnBeginDrag (PointerEventData eventData) {
        isDrag = true;
        startDragHorizontal = rect.horizontalNormalizedPosition; 
    }

    public void OnEndDrag (PointerEventData eventData) {
        //確定停留的index
        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);
        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;
    } 
}

主要是現在unity中很多基礎的組件都需要自己再寫一遍很累就出來共享下。當然這個是有參考大神的博客的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章