Пример #1
0
    public void OnEndDrag(PointerEventData eventData)
    {
        isDrag = false;
        float _st = TimeTool.EndTiming();

        float posX  = scrollrect.horizontalNormalizedPosition;
        int   index = 0;

        //当滑动时间长时,使用该方法
        if (_st > 0.25f)
        {
            float offset = Mathf.Abs(pages[index] - posX);
            for (int i = 0; i < pages.Length; i++)
            {
                float temp = Mathf.Abs(pages[i] - posX);
                if (temp < offset)
                {
                    index  = i;
                    offset = temp;
                }
            }
        }
        //当滑动时间短时,使用该方法
        else
        {
            float offset = pressPoint.x - eventData.position.x;

            //往左滑
            if (offset < 0)
            {
                if (currentPageIndex == 0)
                {
                    index = currentPageIndex;
                }
                else
                {
                    index = currentPageIndex - 1;
                }
            }
            //往右滑
            else
            {
                if (currentPageIndex == pages.Length - 1)
                {
                    index = currentPageIndex;
                }
                else
                {
                    index = currentPageIndex + 1;
                }
            }
        }

        if (index != currentPageIndex)
        {
            currentPageIndex = index;
            OnPageChanged(pages.Length, currentPageIndex);
        }

        targethorizontal = pages[index];
    }