NGUI通过点击按钮来移动面板位置,实现翻页功能

1.首先看一下NGUI自己封装的的SpringPanel,代码如下:

[RequireComponent(typeof(UIPanel))]
[AddComponentMenu("NGUI/Internal/Spring Panel")]
public class SpringPanel : MonoBehaviour
{
public Vector3 target = Vector3.zero;
public float strength = 10f;

public delegate void OnFinished ();
public OnFinished onFinished;

UIPanel mPanel;
Transform mTrans;
float mThreshold = 0f;
UIScrollView mDrag;

/// <summary>
/// Cache the transform.
/// </summary>

void Start ()
{
mPanel = GetComponent<UIPanel>();
mDrag = GetComponent<UIScrollView>();
mTrans = transform;
}

/// <summary>
/// Advance toward the target position.
/// </summary>

void Update ()
{
AdvanceTowardsPosition();
}

/// <summary>
/// Advance toward the target position.
/// </summary>

protected virtual void AdvanceTowardsPosition()
{
float delta = RealTime.deltaTime;

if (mThreshold == 0f)
{
mThreshold = (target - mTrans.localPosition).magnitude * 0.005f;
mThreshold = Mathf.Max(mThreshold, 0.00001f);
}

bool trigger = false;
Vector3 before = mTrans.localPosition;
Vector3 after = NGUIMath.SpringLerp(mTrans.localPosition, target, strength, delta);

if (mThreshold >= Vector3.Magnitude(after - target))
{
after = target;
enabled = false;
trigger = true;
}
mTrans.localPosition = after;

Vector3 offset = after - before;
Vector2 cr = mPanel.clipOffset;
cr.x -= offset.x;
cr.y -= offset.y;
mPanel.clipOffset = cr;

if (mDrag != null) mDrag.UpdateScrollbars(false);
if (trigger && onFinished != null) onFinished();
}

/// <summary>
/// Start the tweening process.
/// </summary>

static public SpringPanel Begin (GameObject go, Vector3 pos, float strength)
{
SpringPanel sp = go.GetComponent<SpringPanel>();
if (sp == null) sp = go.AddComponent<SpringPanel>();
sp.target = pos;
sp.strength = strength;
sp.onFinished = null;
sp.mThreshold = 0f;
sp.enabled = true;
return sp;
}
}

2.接着我们直接就可以用如下代码来实现翻页效果,前提是你要知道每一页滚动之后的scroll位置值,此时每一个item不用添加box,以及drag scroll view,都不用添加,因为我们是为了实现点击翻页,而不是拖拽

  SpringPanel.Begin(m_ScrollView.gameObject, m_ScrollViewTrans[1], 8f);第一个参数为你挂在scrollview的物体,第二个参数为你要移动到的目标位置,第三个参数为移动的速度,

 

转载于:https://www.cnblogs.com/xwwFrank/p/6026866.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值