/// <summary> /// 跳转到指定序号 /// </summary> /// <param name="index_"></param> /// <param name="posType_"></param> /// <returns></returns> public bool JumpToIndex(int index_, JumpPosType posType_) { //if (!m_scrollView.NeedScroll()) // //不需要滚动 // return false; if (index_ < 0 || index_ > m_dataList.Count - 1) { //超出数据范围 return(false); } Vector2 contentPos = m_contentTrans.anchoredPosition; Vector2 maskSize = m_scrollView.maskTrans.sizeDelta; LayoutItem layoutItem = GetLayoutItem(index_); Rect rect = layoutItem.rect; KScrollView.ScrollDir dir = direction; if (dir == KScrollView.ScrollDir.vertical) { float yMin = 0; float yMax = m_contentSize.y - maskSize.y; float y; switch (posType_) { case JumpPosType.TOP: //列表项位于顶部 y = -rect.yMax; //y是负数 break; case JumpPosType.CENTER: //列表项位于中间 y = -rect.center.y - maskSize.y * 0.5f; break; default: //列表项位于底部 y = -rect.yMin - maskSize.y; break; } contentPos.y = Mathf.Clamp(y, yMin, yMax); } else { float xMin = maskSize.x - m_contentSize.x; float xMax = 0; float x; switch (posType_) { case JumpPosType.TOP: //列表项位于顶部 x = -rect.xMin; break; case JumpPosType.CENTER: //列表项位于中间 x = -rect.center.x + maskSize.x * 0.5f; break; default: //列表项位于底部 x = -rect.xMax + maskSize.x; break; } contentPos.x = Mathf.Clamp(x, xMin, xMax); } m_scrollView.StopMovement(); //停止滑动 m_contentTrans.anchoredPosition = contentPos; return(true); }
public void Refresh(Vector2 scrollPosition, KScrollView view) { bool prevVisible = false; bool nextVisible = false; //Log.Debug("scrollPosition " + scrollPosition, this); bool needScroll = view.NeedScroll(); KScrollView.ScrollDir direction = view.direction; if (needScroll) { if (direction == KScrollView.ScrollDir.vertical) { if (scrollPosition.y > 0) { nextVisible = true; } if (scrollPosition.y < 1) { prevVisible = true; } } else { if (scrollPosition.x > 0) { prevVisible = true; } if (scrollPosition.x < 1) { nextVisible = true; } } } else { //内容比遮罩短时, 除非强行拽否则不显示箭头 //需要配合scrollrect的修改 if (direction == KScrollView.ScrollDir.vertical) { if (scrollPosition.y > 1) //强行往下拽, 显示下箭头 { nextVisible = true; } if (scrollPosition.y < 0) //强行往上拽, 显示上箭头 { prevVisible = true; } } else { if (scrollPosition.x > 1) { prevVisible = true; } if (scrollPosition.x < 0) { nextVisible = true; } } } if (m_prevImage) { m_prevImage.Visible = prevVisible; } if (m_nextImage) { m_nextImage.Visible = nextVisible; } }