/// <summary> /// 设置页数 /// </summary> /// <param name="page">页数</param> /// <param name="immediately">是否瞬间跳转</param> private void setPage(int page, bool immediately = false) { //编辑器模式强制为true if (ShineSetting.isEditor) { immediately = true; } int tmpPage = page; page = _loop ? MathUtils.absMod(page, _totalPage) : MathUtils.clamp(page, 0, _totalPage - 1); if (_curPage != page && _newPageAction != null) { _newPageAction(page); } _curPage = page; float scrollPos; if (_scrollType == SScrollType.Vertical) { scrollPos = _loop ? tmpPage * (_row * -_gridCoverHeight - _pageAddSpace) : _curPage * (_row * -_gridCoverHeight - _pageAddSpace); } else { scrollPos = _loop ? tmpPage * (_column * _gridCoverWidth + _pageAddSpace) : _curPage * (_column * _gridCoverWidth + _pageAddSpace); } killChangePageTween(); if (immediately) { setScrollPos(scrollPos); } else { //寻找最近位置 if (_loop) { float distance = _maxRealPos - _minRealPos; scrollPos = getNearPos(_scrollPos, scrollPos, scrollPos + distance, scrollPos - distance); } _changePageTweenIndex = Tween.normal.create(_scrollPos, scrollPos, ChangePageTime, setScrollPos, killChangePageTween); } }
private void refreshGrid(int index) { /** 滚动的格子数 */ int scrollNum; /** 滚动的页数 */ int scrollPageNum; /** 滚动的偏移量 */ float offset; /** 翻页影响的滚动偏移量 */ float offsetPage; /** 实际滚动的像素数(即不包括拉到头之后再继续拉的距离) */ float realScrollPos = _loop ? _scrollPos : MathUtils.clamp(_scrollPos, _minRealPos, _maxRealPos); int dataRow; int dataIndex; int curRow; int curColumn; int tmpColumnOrRowNum; int tmpChangePageNum; Vector3 tmpPos; if (_scrollType == SScrollType.Vertical) { scrollPageNum = (int)Math.Floor(realScrollPos / (-_gridCoverHeight * _row - _pageAddSpace)); tmpColumnOrRowNum = (int)Math.Floor((realScrollPos - scrollPageNum * (-_gridCoverHeight * _row - _pageAddSpace)) / -_gridCoverHeight); if (tmpColumnOrRowNum > _row) { tmpColumnOrRowNum = _row; } scrollNum = scrollPageNum * _row + tmpColumnOrRowNum; tmpChangePageNum = _row - scrollNum + scrollPageNum * _row; offset = scrollNum * -_gridCoverHeight - scrollPageNum * _pageAddSpace - _scrollPos; int startRow = index / _column; curRow = MathUtils.absMod(startRow - scrollNum, _row + 1); curColumn = index % _column; offsetPage = curRow < tmpChangePageNum ? 0 : -_pageAddSpace; RectTransform objTransform = _gridList[index]; tmpPos = objTransform.anchoredPosition; tmpPos.y = curRow * -_gridCoverHeight + offset + offsetPage; objTransform.anchoredPosition = tmpPos; dataRow = startRow + (int)Math.Floor((float)(_row - startRow + scrollNum) / (_row + 1)) * (_row + 1); dataIndex = dataRow * _column + curColumn; if (_loop) { dataIndex = MathUtils.absMod(dataIndex, (int)Math.Ceiling((float)_dataList.size() / (_row * _column)) * _row * _column); } if (dataIndex >= _dataList.size()) { setData(index, -1); } else { setData(index, dataIndex); } } else { scrollPageNum = (int)Math.Floor(realScrollPos / (_gridCoverWidth * _column + _pageAddSpace)); tmpColumnOrRowNum = (int)Math.Floor((realScrollPos - scrollPageNum * (_gridCoverWidth * _column + _pageAddSpace)) / _gridCoverWidth); if (tmpColumnOrRowNum > _column) { tmpColumnOrRowNum = _column; } scrollNum = scrollPageNum * _column + tmpColumnOrRowNum; tmpChangePageNum = _column - scrollNum + scrollPageNum * _column; offset = scrollNum * _gridCoverWidth + scrollPageNum * _pageAddSpace - _scrollPos; int startColumn = index / _row; curColumn = MathUtils.absMod(startColumn - scrollNum, _column + 1); curRow = index % _row; offsetPage = curColumn < tmpChangePageNum ? 0 : _pageAddSpace; RectTransform objTransform = _gridList[index]; tmpPos = objTransform.anchoredPosition; tmpPos.x = curColumn * _gridCoverWidth + offset + offsetPage; objTransform.anchoredPosition = tmpPos; dataRow = startColumn + (int)Math.Floor((float)(_column + 1 - 1 - startColumn + scrollNum) / (_column + 1)) * (_column + 1); dataIndex = dataRow * _row + curRow; if (_loop) { dataIndex = MathUtils.absMod(dataIndex, (int)Math.Ceiling((float)_dataList.size() / (_row * _column)) * _row * _column); } dataIndex = _column * (dataIndex % _row) + dataIndex / _row + dataIndex / (_row * _column) * (_row - 1) * _column; //横向滚动的每页数据也要采用竖向排布 if (dataIndex >= _dataList.size()) { setData(index, -1); } else { setData(index, dataIndex); } } }
private void setScrollPos(float pos) { _scrollPos = _loop ? _minRealPos + MathUtils.absMode(pos - _minRealPos, _maxRealPos - _minRealPos) : MathUtils.clamp(pos, _minRealPos - _maxOutDistance, _maxRealPos + _maxOutDistance); int len = _gridList.Length; for (var i = 0; i < len; i++) { refreshGrid(i); } }