博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NGUI通过点击按钮来移动面板位置,实现翻页功能
阅读量:6952 次
发布时间:2019-06-27

本文共 2091 字,大约阅读时间需要 6 分钟。

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

你可能感兴趣的文章
为什么自己写的组件库被引用总是报错——详解webpack的library和libraryTarget
查看>>
智能合约开发新趋势【2019】
查看>>
我的第一篇博客
查看>>
MSG_OOB unixc
查看>>
面试常见问题
查看>>
React中Fetch之cors跨域请求的使用
查看>>
在Developerkit开发板上运行blink例程
查看>>
【董天一】关于IPFS的热门问题
查看>>
IDEA 2018 搭建 Spring MVC helloworld
查看>>
蚂蚁的开放:想办法摸到10米的篮筐
查看>>
深入解析Immutable及 React 中实践
查看>>
【实操手册】如何把一场直播录制下来?
查看>>
JavaWEB开发10——Cookie&Session
查看>>
Zabbix 3.x 升级到 Zabbix 4.x
查看>>
推荐一个以动画效果显示github提交记录的黑科技工具:Gource
查看>>
快速探索,音视频技术不再神秘
查看>>
.NET开发技巧——从Winform穿越到WPF
查看>>
2135亿背后的双11项目协作怎么玩?
查看>>
DRDS SQL 审计与分析——全面洞察 SQL 之利器
查看>>
微信小程序:模板消息推送实现
查看>>