博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIScrollView 滑动复位
阅读量:6476 次
发布时间:2019-06-23

本文共 3271 字,大约阅读时间需要 10 分钟。

需求

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

分析

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

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

演示

 

以前做法

以前是保存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
("GridPanel"); GridPanel_ScrollView = GridPanel.GetComponent
(); 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代码如下

/// /// 可以对UIPanel进行滚动复位/// 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
("GridPanel"); GridPanelResetHelper = new CUIPanelResetHelper(GridPanel); } public override void OnOpen(params object[] args) { base.OnOpen(args); //打开前 重设Scrollview的属性到初始 GridPanelResetHelper.ResetScroll(); }}

UIScrollView

uiscrollview中的属性

/// /// Whether the dragging will be restricted to be within the scroll view's bounds./// public bool restrictWithinPanel = true;/// /// Whether dragging will be disabled if the contents fit./// public bool disableDragIfFits = false;
http://www.cnblogs.com/zhaoqingqing/p/4916930.html
你可能感兴趣的文章
easyui treegrid逐步加载
查看>>
GraphicsLab Project之辉光(Glare,Glow)效果 【转】
查看>>
<转>Python: __init__.py 用法
查看>>
Linux Curl命令
查看>>
046 SparlSQL中的函数
查看>>
-27979 LoadRunner 错误27979 找不到请求表单 Action.c(73): Error -27979: Requested form not found...
查看>>
[LeetCode] Minimum Depth of Binary Tree
查看>>
,net运行框架
查看>>
Java 中 Emoji 的正则表达式
查看>>
Mixin Network第一届开发者大赛作品介绍- dodice, diceos和Fox.one luckycoin
查看>>
安卓Glide(4.7.1)使用笔记 01 - 引入项目
查看>>
中金易云:为出版社找到下一本《解忧杂货店》
查看>>
Flex布局
查看>>
Material Design之 AppbarLayout 开发实践总结
查看>>
Flutter之MaterialApp使用详解
查看>>
DataBinding最全使用说明
查看>>
原生Js交互之DSBridge
查看>>
Matlab编程之——卷积神经网络CNN代码解析
查看>>
白洋淀周末游
查看>>
三篇文章了解 TiDB 技术内幕 —— 说计算
查看>>