Ejemplo n.º 1
0
    public void GotoPage(int pageIndex, bool anim = true)
    {
        _isDrag = false;
        if (_currentPage != pageIndex)
        {
            if (_isLoop)
            {
                if (pageIndex < 0)
                {
                    pageIndex = _totalPage;
                }
                else if (pageIndex > _totalPage)
                {
                    pageIndex = 0;
                }
            }
            else
            {
                if (pageIndex < 0)
                {
                    pageIndex = 0;
                }
                else if (pageIndex > _totalPage)
                {
                    pageIndex = _totalPage;
                }
            }

            _currentPage = pageIndex;

            RectTransform child = null;
            foreach (Transform temp in content)
            {
                PageItem item = temp.gameObject.GetComponent <PageItem>();
                if (item.index == pageIndex)
                {
                    child = temp as RectTransform;
                    break;
                }
            }
            if (null == child)
            {
                return;
            }

            Vector2 endPos = -child.anchoredPosition;
            _endPos          = endPos;
            _isUpdateEnabled = true;
            _onPageChange.Invoke(_currentPage);
            if (_pageIndicator)
            {
                _pageIndicator.ShowPage(child.GetComponent <PageItem>().index);
            }

            if (!anim)
            {
                _isUpdateEnabled = false;
                SetContentAnchoredPosition(_endPos);
            }
        }
    }