Пример #1
0
    private void OnDragFinished()
    {
        var dragOffset = mPanel.clipOffset.x - panelStartOffset;

        //拖动方向
        mFinisedSvDragDir    = dragOffset > 0 ? DragmoveDir.Left : dragOffset < 0 ? DragmoveDir.Right : DragmoveDir.None;
        intFinishedSvDragDir = mFinisedSvDragDir == DragmoveDir.Left ? 1 : mFinisedSvDragDir == DragmoveDir.Right ? -1 : 0;
        //Debug.LogError("Drag:" + mFinisedSvDragDir);

        #region 跳转页下标,页位置计算
        var pageNum = 0;
        var goToNextPageDistance = pageColumnLimit * cellSize;
        var offsetDistance       = Mathf.Abs((int)(mPanel.clipOffset.x - panelStartOffset));
        if (mMovement == UIScrollView.Movement.Horizontal)
        {
            pageNum = (offsetDistance / goToNextPageDistance) + 1;
            pageNum = offsetDistance >= goToNextPageDistance + goToNextPageDistance / 2 ? pageNum - 1 : pageNum;
        }

        var dragDistance = Mathf.Abs(dragOffset);
        int willGotoPageIndex;
        if (dragDistance >= minDragMoveDistance)// 限制1:翻页的最短距离限制
        {
            if (mFinisedSvDragDir == DragmoveDir.Left)
            {
                willGotoPageIndex = curPageIndex + pageNum;
                if (IsNeedFirstLastLimit && willGotoPageIndex >= pageTotalNum)//限制2:首尾翻页限制
                {
                    Extensions.LogAttentionTip(string.Format("当前第{0}页,不支持左拖翻页到{1}", curPageIndex, willGotoPageIndex));
                    willGotoPageIndex = pageTotalNum - 1;
                }
                curPageIndex = willGotoPageIndex;
            }
            if (mFinisedSvDragDir == DragmoveDir.Right)
            {
                willGotoPageIndex = curPageIndex - pageNum;


                if (IsNeedFirstLastLimit && willGotoPageIndex < 0) //限制2:首尾翻页限制
                {
                    Extensions.LogAttentionTip(string.Format("当前第{0}页,不支持右拖翻页{1}", curPageIndex, willGotoPageIndex));
                    willGotoPageIndex = 0;
                }
                curPageIndex = willGotoPageIndex;
            }
        }
        else
        {
            Extensions.LogAttentionTip(string.Format("拖动距离{0}太短,不足以翻页", dragDistance));
        }
        //Debug.LogError(curPageIndex);


        var finalmoveTo = -1 * curPageIndex * pageColumnLimit * cellSize;
        SpringPanel.Begin(mPanel.gameObject, new Vector3(finalmoveTo, 0, 0), 8f);

        #endregion
        //移动方向
        mFinisedSvMoveDir    = mPanel.transform.localPosition.x - finalmoveTo > 0 ? DragmoveDir.Left : mPanel.transform.localPosition.x - finalmoveTo < 0 ? DragmoveDir.Right : DragmoveDir.None;
        intFinishedSvMoveDir = mFinisedSvMoveDir == DragmoveDir.Left ? 1 : mFinisedSvMoveDir == DragmoveDir.Right ? -1 : 0;
        //Debug.LogError("Move:" + mFinisedSvMoveDir);
        //Debug.LogError("Move int:" + intFinishedSvMoveDir);

        //CheckMove();
    }
    /// <summary>
    /// 检测并移动
    /// </summary>
    private void CheckCellMove()
    {
        int rowLimit  = mMovement == UIScrollView.Movement.Vertical ? mPanelRowLimit + extraShowNum : mPanelRowLimit;
        int lineLimit = mMovement == UIScrollView.Movement.Horizontal ? mPanelColumnLimit + extraShowNum : mPanelColumnLimit;

        if (cellGoList.Count <= lineLimit)
        {
            return;
        }

        int curLastColIndex, curLastShowDateColIndex;
        int cellIndex, moveColIndex, dataIndex = 0;

        bool  isCanMove = true;
        float cellX     = 0;

        if (mMovement == UIScrollView.Movement.Horizontal)
        {
            curFirstColIndex = curFirstColIndex % lineLimit;
            curLastColIndex  = (curFirstColIndex + lineLimit - 1) % lineLimit;

            curFirstShowDataColIndex = curFirstShowDataColIndex % mDataColumnLimit;
            curLastShowDateColIndex  = (curFirstShowDataColIndex + mPanelColumnLimit) % mDataColumnLimit;

            int         moveDir = mPanel.clipOffset.x - panelStartOffset > 0 ? 1 : mPanel.clipOffset.x - panelStartOffset < 0 ? -1 : 0;
            DragmoveDir dragDir = moveDir == 1 ? DragmoveDir.Left : moveDir == -1 ? DragmoveDir.Right : DragmoveDir.None;
            if (dragDir == DragmoveDir.None)
            {
                return;
            }

            //需要移动的列index
            moveColIndex = dragDir == DragmoveDir.Left ? curFirstColIndex : dragDir == DragmoveDir.Right ? curLastColIndex : -1;

            cellX = cellGoList[moveColIndex].transform.localPosition.x;
            cellX = cellX - mPanel.clipOffset.x;
            //左拖动 offset变大 cell右移 x增加
            if (IsAllOutHoriPanel(cellX))
            {
                for (int hangIndex = 0; hangIndex < rowLimit; hangIndex++)
                {
                    cellIndex = hangIndex * lineLimit + moveColIndex;
                    cellX     = cellGoList[cellIndex].transform.localPosition.x + moveDir * lineLimit * cellSize;

                    if (dragDir == DragmoveDir.Left)
                    {
                        dataIndex = curLastShowDateColIndex + extraShowNum;
                        if (dataIndex >= mDataColumnLimit) //到尾限制
                        {
                            return;
                        }
                    }
                    else if (dragDir == DragmoveDir.Right)
                    {
                        dataIndex = curFirstShowDataColIndex - extraShowNum;
                        if (dataIndex < 0) //到头限制
                        {
                            return;
                        }
                    }
                    dataIndex = hangIndex * mDataColumnLimit + dataIndex;
                    onUpdateItem(cellGoList[cellIndex], dataIndex);
                    cellGoList[cellIndex].transform.SetLocalX(cellX);
                }

                if (dragDir == DragmoveDir.Left)
                {
                    curFirstShowDataColIndex++;
                    curFirstColIndex++;
                }
                else if (dragDir == DragmoveDir.Right)
                {
                    curFirstShowDataColIndex--;
                    curFirstColIndex = curLastColIndex;
                }
            }
        }
        if (mMovement == UIScrollView.Movement.Vertical)
        {
            //todo
        }
    }