Ejemplo n.º 1
0
        /// <summary>
        /// 初始化此控件.
        /// </summary>
        public PageView Init(int page = 0)
        {
            if (horizontal)
            {
                content.pivot = new Vector2(0, 0.5f); //内容的原点在最左边.
            }
            else if (vertical)
            {
                content.pivot = new Vector2(0.5f, 1f); //内容的原点在最上边.
            }
            m_totalPage = content.childCount - 1;


//			Vector2 itemPos = Vector2.one;
//			Vector2 itemSize = Vector2.one;
//			Vector2 itemAnchorMin = Vector2.one;
//			Vector2 itemAnchorMax = Vector2.one;
//			Vector2 itemPivot = Vector2.one;
//			Vector2 itemOffsetMin = Vector2.one;
//			Vector2 itemOffsetMax = Vector2.one;
//			List<Vector2> pos = new List<Vector2>();
//
//			if (content.childCount > 0) {
//				RectTransform child = content.GetChild (0).transform as RectTransform;
//				itemSize = child.sizeDelta;
//				itemPos = child.anchoredPosition;
//				itemAnchorMin = child.anchorMin;
//				itemAnchorMax = child.anchorMax;
//				itemPivot = child.pivot;
//				itemOffsetMin = child.offsetMin;
//				itemOffsetMax = child.offsetMax;
//				foreach (RectTransform ch in content)
//				{
//					pos.Add(ch.anchoredPosition);
//				}
//			}

            LayoutGroup group = transform.GetComponentInChildren <LayoutGroup>();

            if (group && Application.isPlaying)
            {
                group.enabled = false;
            }

            int i = 0;

            foreach (RectTransform child in content)
            {
//				itemPivot = child.pivot;
//				child.offsetMin = itemOffsetMin;
//				child.offsetMax = itemOffsetMax;
//				child.anchorMin = itemAnchorMin;
//				child.anchorMax = itemAnchorMax;
//				child.sizeDelta = itemSize;
//				child.anchoredPosition = pos[i];

                PageItem item = child.GetComponent <PageItem>();
                if (item == null)
                {
                    item = child.gameObject.AddComponent <PageItem>();
                }
                item.index = i;

//				LayoutGroup childGroup = item.GetComponent<LayoutGroup>();
//				if(childGroup && childGroup.enabled){
//					childGroup.enabled = false;
//					childGroup.enabled = true;
//				}
                ++i;
            }


            m_end = -((RectTransform)content.GetChild(0).transform).anchoredPosition;
            SetContentAnchoredPosition(m_end);

            if (pageIndicator)
            {
                pageIndicator.iPage = this;
                pageIndicator.Build(totalPage + 1);
            }
            return(this);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 切换到哪一页.
        /// </summary>
        /// <param name="pageIndex">从0开始.</param>
        /// <param name="anim">是否显示翻页动画.默认为true.</param>
        public void GotoPage(int pageIndex, bool anim = true)
        {
            m_drag = false;
            if (m_currentPage != pageIndex)
            {
                if (isLoop)
                {
                    if (pageIndex < 0)
                    {
                        pageIndex = m_totalPage;
                    }
                    else if (pageIndex > m_totalPage)
                    {
                        pageIndex = 0;
                    }
                }
                else
                {
                    if (pageIndex < 0)
                    {
                        pageIndex = 0;
                    }
                    else if (pageIndex > m_totalPage)
                    {
                        pageIndex = m_totalPage;
                    }
                }

                m_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 (child == null)
                {
                    return;
                }

                Vector2 endPos = -child.anchoredPosition;
                m_end         = endPos;
                _enableUpdate = true;
                onPageChange.Invoke();
                if (pageIndicator)
                {
                    pageIndicator.ShowPage(child.GetComponent <PageItem>().index);
                }

                if (!anim)
                {
                    _enableUpdate = false;
                    SetContentAnchoredPosition(m_end);
                }
            }
        }