zoukankan      html  css  js  c++  java
  • UIPanelResetHelper(UIScrollView滚动复位)

    原理

    如果我们的UI中有滑动列表,并且列表比较长,那么不知道你们是否有这样需求,每次页面打开时,列表的滑动状态都恢复到默认状态。

    如果要复位,其实就是修改UIPanel 的属性到初始状态。此组件做的工作就是在初始化时把UIPanel的属性保存起来,在需要时还原初始值,达到复位效果。

    组件代码

    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();
        }
    }
  • 相关阅读:
    非常简洁的js图片轮播
    js广告图片轮播
    图片轮播
    分享到QQ空间、新浪微博、腾讯微博和人人网
    五星简单操作
    Struts2 多文件上传
    CSS3实践之路(六):CSS3的过渡效果(transition)与动画(animation)
    JavaScript中的数据类型
    JavaScript中对象是否需要加引号?
    变量提升(hoisting)
  • 原文地址:https://www.cnblogs.com/zhaoqingqing/p/5546927.html
Copyright © 2011-2022 走看看