//设置中间 public void SetCenterItem(EnhanceScrollViewItem selectItem) { if (!mChangeItem) { return; } if (mCurCenterItem == selectItem) { return; } mChangeItem = false; mPreCenterItem = mCurCenterItem; mCurCenterItem = selectItem; bool isRight = false; if (selectItem.transform.localPosition.x > m_CenterXValue) { isRight = true; } SortEnhanceItem(); int moveIndex = Mathf.Abs(mPreCenterItem.RealIndex - selectItem.RealIndex); float intervalValue = 0.0f; if (isRight) { intervalValue = -moveIndex * mIntervalFactor; } else { intervalValue = moveIndex * mIntervalFactor; } PrepareTween(m_CurHorizontalSliderValue, m_CurHorizontalSliderValue + intervalValue, true); }
//更新界面,sliderValue为整个界面的滑动条值,思路是通过整个界面的滑动条值,计算子界面的滑动条值 //sliderValue 为整个界面的sliderValue,大小无限制 private void UpdateScrollView(float sliderValue) { for (int i = 0; i < mItemList.Count; i++) { EnhanceScrollViewItem item = mItemList[i]; float childSliderVaue = sliderValue + item.LocalSliderValue; item.SliderValue = childSliderVaue; float xPosValue = GetXPosValue(childSliderVaue); float scaleValue = GetScaleValue(childSliderVaue); float depthValue = GetDepthValue(childSliderVaue); item.UpdateInScrollView(xPosValue, YFixedPositionValue, scaleValue, depthValue, mDepthFactor, mItemList.Count); } }
private void Start() { //规则 极左-极右(0-1) mChangeItem = true; int count = mItemList.Count; m_CenterIndex = count / 2; if (count % 2 == 0) { m_CenterIndex = count / 2 - 1; } mIntervalFactor = (Mathf.RoundToInt((1f / count) * 10000f)) * 0.0001f; int index = 0; for (int i = 0; i < count; i++) { mItemList[i].StartIndex = i; mItemList[i].LocalSliderValue = mIntervalFactor * (m_CenterIndex + index); mItemList[i].SetSelectState(false); mItemList[i].Init(this); index++; } //越界的情况下设置中心的item if (StartCenterIndex < 0 || StartCenterIndex > count) { StartCenterIndex = m_CenterIndex; } //排序 mSortItemList = new List <EnhanceScrollViewItem>(mItemList.ToArray()); mTotalHorizontalWidth = CellWidth * count; mCurCenterItem = mSortItemList[StartCenterIndex]; m_CurHorizontalSliderValue = 0.5f - mCurCenterItem.LocalSliderValue; m_CenterXValue = PositionCurve.Evaluate(0.5f) * mTotalHorizontalWidth; PrepareTween(0, m_CurHorizontalSliderValue, false); }
//拖拽结束 public void OnDragEnd() { float minDis = Mathf.Infinity; int minIndex = 0; for (int i = 0; i < mItemList.Count; i++) { float distance = Mathf.Abs(mItemList[i].transform.localPosition.x - m_CenterXValue); if (distance < minDis) { minDis = distance; minIndex = i; } } float target = 0; EnhanceScrollViewItem item = mItemList[minIndex]; float remainder = item.SliderValue % mIntervalFactor; bool isEven = mItemList.Count % 2 == 0; float start = m_CurHorizontalSliderValue; //在中心的右边 if (item.transform.localPosition.x > m_CenterXValue) { if (m_CurHorizontalSliderValue > 0) { if (!isEven) { start = m_CurHorizontalSliderValue + mIntervalFactor / 2; } target = start + (mIntervalFactor - m_CurHorizontalSliderValue % mIntervalFactor) - mIntervalFactor; } else { if (!isEven) { start = m_CurHorizontalSliderValue + mIntervalFactor / 2; } target = start - (mIntervalFactor + m_CurHorizontalSliderValue % mIntervalFactor); } } else//左边 { if (m_CurHorizontalSliderValue > 0) { if (!isEven) { start = m_CurHorizontalSliderValue - mIntervalFactor / 2; } target = start - m_CurHorizontalSliderValue % mIntervalFactor + mIntervalFactor; } else { if (!isEven) { start = m_CurHorizontalSliderValue - mIntervalFactor / 2; } target = start - m_CurHorizontalSliderValue % mIntervalFactor; } } mPreCenterItem = mCurCenterItem; mCurCenterItem = item; PrepareTween(m_CurHorizontalSliderValue, target, true); mChangeItem = false; }