UIScrollView 滑动复位

本文介绍了一种在UI设计中使滑动列表每次打开都返回初始位置的方法。通过使用CUIPanelResetHelper组件,避免了直接修改UIPanel的baseClipRegion属性,实现了高效且官方推荐的滑动列表复位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求

在每次打开界面滑动列表都是复位状态(未滑动)。

分析

在制作滑动列表时常常会结合UIPanel和UIScrollView

要让滑动列表回到未滑动时的位置,那么就需要改变Panel的Clipping和transform的position

演示

scrollview_1

 

以前做法

以前是保存Panel的初始信息,每时打开面板时再还原

public class CUIShopVIP : CUIController
{
    private UIPanel GridPanel;
    private UIScrollView GridPanel_ScrollView;
    private float BakGridPanel_Y;
    private Vector4 BakGridPanel_ClipRegion;

    public override void OnInit()
    {
        base.OnInit();
        //...... 初始化代码

        GridPanel = GetControl<UIPanel>("GridPanel");
        GridPanel_ScrollView = GridPanel.GetComponent<UIScrollView>();

        BakGridPanel_Y = GridPanel.transform.GetLocalPositionY();
        BakGridPanel_ClipRegion = GridPanel.baseClipRegion;
    } 
    
    public override void OnOpen(params object[] args)
    {
        base.OnOpen(args);

        //打开前 重设Scrollview的属性到初始

        GridPanel.baseClipRegion = BakGridPanel_ClipRegion;//NOTE 不建议直接修改此值
        GridPanel_ScrollView.UpdatePosition();
        GridPanel_ScrollView.ResetPosition();
        GridPanel.transform.SetLocalPositionY(BakGridPanel_Y);   
    }
}

之前的做法是 修改 Panel的 localPosition 和 Panel的 baseClipRegion,但官方不建议直接修改baseClipRegion。然后更新ScrollView的信息

/// <summary>
/// Clipping position (XY) and size (ZW).
/// Note that you should not be modifying this property at run-time to reposition the clipping. Adjust clipOffset instead.
/// </summary>

public Vector4 baseClipRegion

 

编写组件

现在做了一个PanelResetHelper,修改Panel的 clipOffsetlocalPosition 而不直接修改baseClipRegion

/// <summary>
/// Clipping area offset used to make it possible to move clipped panels (scroll views) efficiently.
/// Scroll views move by adjusting the clip offset by one value, and the transform position by the inverse.
/// This makes it possible to not have to rebuild the geometry, greatly improving performance.
/// </summary>

public Vector2 clipOffset

原理

和之前做法一样,初始化时保存Panel的属性,界面打开时,再还原值。

组件源码

Helper代码如下

/// <summary>
/// 可以对UIPanel进行滚动复位
/// </summary>
public class CUIPanelResetHelper
{
    private UIPanel _panel;
    private Vector2 _initPanelClipOffset;
    private Vector3 _initPanelLocalPos;

    public CUIPanelResetHelper(UIPanel uiPanel)
    {
        _panel = uiPanel;
        _initPanelClipOffset = _panel.clipOffset;
        _initPanelLocalPos = _panel.cachedTransform.localPosition;
    }

    public void ResetScroll()
    {
        _panel.clipOffset = _initPanelClipOffset;
        _panel.cachedTransform.localPosition = _initPanelLocalPos;
    }
}

组件用法

public class CUIShopVIP : CUIController
{
    private UIPanel GridPanel;
    private CUIPanelResetHelper GridPanelResetHelper;

    public override void OnInit()
    {
        base.OnInit();
        //...... 初始化代码
        GridPanel = GetControl<UIPanel>("GridPanel");
        GridPanelResetHelper = new CUIPanelResetHelper(GridPanel);
    }

    public override void OnOpen(params object[] args)
    {
        base.OnOpen(args);
        
        //打开前 重设Scrollview的属性到初始
        GridPanelResetHelper.ResetScroll();
    }
}

UIScrollView

uiscrollview中的属性

/// <summary>
/// Whether the dragging will be restricted to be within the scroll view's bounds.
/// </summary>
public bool restrictWithinPanel = true;

/// <summary>
/// Whether dragging will be disabled if the contents fit.
/// </summary>
public bool disableDragIfFits = false;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值