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();
    }
}

本文转自赵青青博客园博客,原文链接:http://www.cnblogs.com/zhaoqingqing/p/5546927.html ,如需转载请自行联系原作者

相关文章
|
iOS开发
拖动手势UIPanGestureRecognizer
拖动手势UIPanGestureRecognizer
258 0
拖动手势UIPanGestureRecognizer
SwiftUI—如何给视图添加拖动手势
SwiftUI—如何给视图添加拖动手势
515 0
SwiftUI—如何给视图添加拖动手势
|
iOS开发
iOS ScrollView嵌套tableview左右滑动时禁止上下滑动
iOS ScrollView嵌套tableview左右滑动时禁止上下滑动
1448 0
|
C# Windows
WPF 定时器DispatcherTimer+GetCursorPos 的使用,动态查看屏幕上任一点坐标
原文:WPF 定时器DispatcherTimer+GetCursorPos 的使用,动态查看屏幕上任一点坐标 using System;using System.Collections.Generic;using System.
1022 0
|
C#
WPF DispatcherTimer(定时器应用) 无人触摸60s自动关闭窗口
原文:WPF DispatcherTimer(定时器应用) 无人触摸60s自动关闭窗口 如果无人触摸:60s自动关闭窗口 xmal:部分             OK        你好!    cs:部分 //60s无人操作自动关闭        DispatcherTimer dTi...
1541 0
|
C#
WPF Touch操作滚动条,Window弹跳
原文:WPF Touch操作滚动条,Window弹跳 WPF,用ScrollViewer控件,触屏开发,当滑动到最后时会使整个窗体弹跳一下 原因是因为ScrollViewer触屏操作原生支持惯性,ScrollViewer中的内容滚动到边界是会自动触发Window Bounce(窗体弹跳), 以叫做Panning Feedback(拖动回馈)。
1140 0