NGUI 中有一个关于UIScrollView的Exmaple,其中的一个功能拖动滚动条时总会有一个Item显示在中间的案例。但是拖动起来操不爽很压抑所以需要修改一下。
其中缓动的核心代码是:SpringPanel.Begin(panel.cachedGameObject, offset, 6f);
先看看UIDragScrollView.cs类
void OnDrag(Vector2 delta)
{
if (scrollView && NGUITools.GetActive(this))
{
scrollView.Drag();
scrollView._movePower = delta.y;
}
MovePower = delta.y;//拖动的动力系数
}
void OnDragEnd()
{
scrollView._movePower = MovePower;//拖动结束后把拖动系数返回给scrollView进行缓动
scrollView.OnDragFinished();
}
再看UIScrollView.cs
public void OnDragFinished()
{
int _itemSize = transform.GetComponentInChildren<MyUIWrapContent>().itemSize;
UIPanel panel = transform.GetComponent<UIPanel>();
int _move = ((int)(transform.localPosition.y / _itemSize)) * _itemSize;
if (_movePower > 3 || _movePower < -3)//滑动系数太小回滚
{
_move = _move + 100 * (int)(_movePower / 2);
}
Vector3 offset = -panel.cachedTransform.InverseTransformPoint(transform.position);
if (!canMoveHorizontally) offset.x = panel.cachedTransform.localPosition.x;
if (!canMoveVertically) offset.y = _move;
offset.y = _move;
SpringPanel.Begin(panel.cachedGameObject, offset, 6f);
}
void LateUpdate()
{
if (!Application.isPlaying) return;
float delta = RealTime.deltaTime;
// Fade the scroll bars if needed
if (showScrollBars != ShowCondition.Always && (verticalScrollBar || horizontalScrollBar))
{
bool vertical = false;
bool horizontal = false;
if (showScrollBars != ShowCondition.WhenDragging || mDragID != -10 || mMomentum.magnitude > 0.01f)
{
vertical = shouldMoveVertically;
horizontal = shouldMoveHorizontally;
}
if (verticalScrollBar)
{
float alpha = verticalScrollBar.alpha;
alpha += vertical ? delta * 6f : -delta * 3f;
alpha = Mathf.Clamp01(alpha);
if (verticalScrollBar.alpha != alpha) verticalScrollBar.alpha = alpha;
}
if (horizontalScrollBar)
{
float alpha = horizontalScrollBar.alpha;
alpha += horizontal ? delta * 6f : -delta * 3f;
alpha = Mathf.Clamp01(alpha);
if (horizontalScrollBar.alpha != alpha) horizontalScrollBar.alpha = alpha;
}
}
if (!mShouldMove) return;
// Apply momentum
if (!mPressed)
{
if (mMomentum.magnitude > 0.0001f || mScroll != 0f)
{
//原有的缓动注销掉
/*
if (movement == Movement.Horizontal)
{
mMomentum -= mTrans.TransformDirection(new Vector3(mScroll * 0.05f, 0f, 0f));
}
else if (movement == Movement.Vertical)
{
mMomentum -= mTrans.TransformDirection(new Vector3(0f, mScroll * 0.05f, 0f));
}
else if (movement == Movement.Unrestricted)
{
mMomentum -= mTrans.TransformDirection(new Vector3(mScroll * 0.05f, mScroll * 0.05f, 0f));
}
else
{
mMomentum -= mTrans.TransformDirection(new Vector3(
mScroll * customMovement.x * 0.05f,
mScroll * customMovement.y * 0.05f, 0f));
}
mScroll = NGUIMath.SpringLerp(mScroll, 0f, 20f, delta);
// Move the scroll view
Vector3 offset = NGUIMath.SpringDampen(ref mMomentum, dampenStrength, delta);
// MoveAbsolute(offset);
if (offset.y == 0.1)
{
mMomentum = Vector3.zero;
mScroll = 0f;
if (onStopped != null) onStopped();
return;
}
//Debug.Log("magnitude="+ mMomentum.magnitude + " dampenStrength=" + dampenStrength+" "+ offset);
//Debug.Log("vector a=" + offset + " "+ dampenStrength);
// Restrict the contents to be within the scroll view's bounds
if (restrictWithinPanel && mPanel.clipping != UIDrawCall.Clipping.None)
{
if (NGUITools.GetActive(centerOnChild))
{
if (centerOnChild.nextPageThreshold != 0f)
{
mMomentum = Vector3.zero;
mScroll = 0f;
}
else centerOnChild.Recenter();
}
else
{
RestrictWithinBounds(false, canMoveHorizontally, canMoveVertically);
}
}
if (onMomentumMove != null)
onMomentumMove();
*/
}
else
{
mScroll = 0f;
mMomentum = Vector3.zero;
SpringPanel sp = GetComponent<SpringPanel>();
if (sp != null && sp.enabled) return;
mShouldMove = false;
if (onStoppedMoving != null)
onStoppedMoving();
}
}
else
{
// Dampen the momentum
mScroll = 0f;
NGUIMath.SpringDampen(ref mMomentum, 9f, delta);
}
}