Unity之UGUI实现滑动翻页效果

这个问题真的是老生常谈的事情了,不过在这里还是要说一下,以便以后之需

首先看一下效果图

Unity之UGUI实现滑动翻页效果_第1张图片

Unity之UGUI实现滑动翻页效果_第2张图片

Unity之UGUI实现滑动翻页效果_第3张图片

 

Unity之UGUI实现滑动翻页效果_第4张图片

Unity之UGUI实现滑动翻页效果_第5张图片

最后在Content下面是一些Image

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

public class PageView : MonoBehaviour, IBeginDragHandler, IEndDragHandler {
     ScrollRect rect;                        //滑动组件  
    //public ScrollRect rect2;                        //滑动组件2  

    private float targethorizontal = 0;             //滑动的起始坐标  
    private bool isDrag = false;                    //是否拖拽结束  
    private List posList = new List ();//求出每页的临界角,页索引从0开始  
    private int currentPageIndex = -1;
    public Action OnPageChanged;

    private bool stopMove = true;
    public float smooting = 4;      //滑动速度  
    public float sensitivity = 0;
    private float startTime;

    private float startDragHorizontal; 


    void Awake () {
       // rect = rect2;
        rect = transform.GetComponent ();
       // rect2 = transform.GetComponent();
        float horizontalLength = rect.content.rect.width - GetComponent ().rect.width;
        //float horizontalLength2 = rect2.content.rect.width - GetComponent().rect.width;
        posList.Add (0);
        for(int i = 1; i < rect.content.transform.childCount - 1; i++) {
            posList.Add (GetComponent ().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);
           // rect2.horizontalNormalizedPosition = Mathf.Lerp(rect2.horizontalNormalizedPosition, targethorizontal, t);
            if (t >= 1)
                stopMove = true;
        }
    }

    public void pageTo (int index) {
        if(index >= 0 && index < posList.Count) {
            rect.horizontalNormalizedPosition = posList[index];
            SetPageIndex(index);
        } else {
            Debug.LogWarning ("页码不存在");
        }
    }
    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;
       // startDragHorizontal = rect2.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);
        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之UGUI实现滑动翻页效果_第6张图片

 

 

Unity之UGUI实现滑动翻页效果_第7张图片

Unity之UGUI实现滑动翻页效果_第8张图片

剩下的就没有什么了。。。唯一的缺憾是,我还在添加Toggle。。。

留着以后更新吧...

你可能感兴趣的:(Unity3D)