wangguan
2020-12-02 cf99ef52be344ac7dd3ba28dd51c63dd5de38a4b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
 
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using System;
using DG.Tweening;
 
/// <summary>
/// 直接从网上找到的代码,基本可用。
/// </summary>
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;
    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 BottomMainTab mainTab;
 
 
    void Start()
    {
        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);
        }
     
        currentPageIndex = 2;
    }
 
    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;
        }
        //Debug.Log(rect.horizontalNormalizedPosition);
    }
 
    public void pageTo(int index)
    {
        if (index >= 0 && index < posList.Count)
        {
            rect.DOHorizontalNormalizedPos(posList[index], 0.4f);
            //rect.horizontalNormalizedPosition = posList[index];
            SetPageIndex(index);
        }
    }
    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)
    {
        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);
            //Debug.Log("temp " + temp);
            //Debug.Log("i" + i);
            if (temp < offset)
            {
                index = i;
                offset = temp;
            }
            //Debug.Log("index " + index);
        }
 
        // 一次最多移动一个Page.
        if( Math.Abs(currentPageIndex - index) > 1)
        {
            if (currentPageIndex > index)
                index = currentPageIndex - 1;
            else
                index = currentPageIndex + 1;
        }
 
        //Debug.Log(index);
        SetPageIndex(index);
        GetIndex(index);
        targethorizontal = posList[index]; //设置当前坐标,更新函数进行插值  
        isDrag = false;
        startTime = 0;
        stopMove = false;
 
    }
 
    public void GetIndex(int index)
    {
        if (this.mainTab)
            mainTab.onPageSwith(index);
        /*
        var toogle = toggleList.GetChild(index).GetComponent<Toggle>();
        toogle.isOn = true;*/
    }
}