UIScrollView 滑动复位

简介:



需求

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

分析

在制作滑动列表时常常会结合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的 clipOffset和 localPosition 而不直接修改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;
复制代码
相关文章
|
7月前
|
XML 存储 Android开发
Android系统 添加动态控制USB TP触摸方向、触摸唤醒
Android系统 添加动态控制USB TP触摸方向、触摸唤醒
251 0
|
Windows
实现vb滚轮滑动
实现vb滚轮滑动
85 0
LabVIEW鼠标滚轮实现波形放大缩小(zoom)功能
实现功能:将鼠标放在波形图曲线上,滚轮可以实现波形放大缩小功能。 代码思想:注册鼠标滚轮事件,滚轮时改变波形图横纵坐标最大值和最小值。
158 0
|
Android开发
4.2 锁屏小部件亮屏时不显示边框
4.2 锁屏小部件亮屏时不显示边框
119 0
4.2 锁屏小部件亮屏时不显示边框
UIScrollView滑动选页
UIScrollView滑动选页
81 0
UIView随手指的移动
UIView随手指的移动
59 0
触屏事件-上下左右滑动
window.touchMove=function(){ // 用于纪录触摸开始时的坐标 var startX=0,startY=0, //创建一个变量,用于保存触摸方向 touchDirection=""; //创建一个对象,用于保存滑动事件 var funcs = {}; if(arguments.
1326 0