标签:
如果我们的UI中有滑动列表,并且列表比较长,那么不知道你们是否有这样需求,每次页面打开时,列表的滑动状态都恢复到默认状态。
如果要复位,其实就是修改UIPanel 的属性到初始状态,那么我们可以在初始化把Panel的属性保存起来,在需要还原它。
using UnityEngine; using System.Collections.Generic; /// <summary> /// 方便对UIPanel进行滚动复位 /// 用法: /// var panelResetHelper = UIPanelResetHelper.Create(m_uiPanel); /// 页面重新打开时调用:panelResetHelper.ResetScroll(); /// by 赵青青 /// </summary> public class UIPanelResetHelper { public UIPanel m_panel { get; private set; } public Vector2 m_initPanelClipOffset { get; private set; } public Vector3 m_initPanelLocalPos { get; private set; } public UIPanelResetHelper(UIPanel uiPanel) { if (uiPanel == null) { Debug.LogWarning("需要UIPanel,却传入了NULL,请检查"); return; } m_panel = uiPanel; m_initPanelClipOffset = m_panel.clipOffset; m_initPanelLocalPos = m_panel.cachedTransform.localPosition; } public void ResetScroll() { if (m_panel == null) return; m_panel.clipOffset = m_initPanelClipOffset; m_panel.cachedTransform.localPosition = m_initPanelLocalPos; } public static UIPanelResetHelper Create(Transform uiPanelTrans) { UIPanel uiPanel = uiPanelTrans.GetComponent<UIPanel>(); return Create(uiPanel); } public static UIPanelResetHelper Create(UIPanel uiPanel) { return new UIPanelResetHelper(uiPanel); } }
在打开页面时,调用resetscroll。
using UnityEngine; using System.Collections; public class UIPayList : MonoBehaviour { public Transform m_ListPanel; private UIPanelResetHelper m_PanelResetHelper; public void BindContrller() { //TODO 其它的绑定代码 if (m_ListPanel) { m_PanelResetHelper = UIPanelResetHelper.Create(m_ListPanel); } } public void OnOpen() { //打开时,对列表进行复位 ResetScroll(); } public void ResetScroll() { //NOTE 重设Scrollview的位置 if (m_PanelResetHelper != null) m_PanelResetHelper.ResetScroll(); } }
标签:
原文地址:http://www.cnblogs.com/zhaoqingqing/p/5546927.html