需求

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

分析

在制作滑动列表时常常会结合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<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;

UIScrollView 滑动复位的更多相关文章

  1. 固定UIScrollView滑动的方向

    固定UIScrollView滑动的方向 一般而言,我们通过这两个参数CGRectMake以及contentSize就可以自动的让UIScrollView只往一个方向滚动.但我遇到过非常奇葩的情况,那就 ...

  2. UIPanelResetHelper(UIScrollView滚动复位)

    原理 如果我们的UI中有滑动列表,并且列表比较长,那么不知道你们是否有这样需求,每次页面打开时,列表的滑动状态都恢复到默认状态. 如果要复位,其实就是修改UIPanel 的属性到初始状态.此组件做的工 ...

  3. UIScrollView 滑动试图

    UIScrollView --->UIView //创建UIScrollView testScrollView=[[UIScrollView alloc]init]; testScrollVie ...

  4. iOS中如何使定时器NSTimer不受UIScrollView滑动所影响

    以下是使用 scheduledTimerWithTimeInterval 方法来实现定时器 - (void)addTimer { NSTimer scheduledTimerWithTimeInter ...

  5. IOS开发之--UIScrollView pagingEnabled自定义翻页宽度

    用到UIScrollview的翻页效果时,有时需要显示一部分左右的内容,但是UIScrollView的PagingEnabled只能翻过整页,下面几个简单的设置即可实现 技术点: 1. 创建一个继承U ...

  6. IOS开发之UIScrollVIew运用

    UIScrollView可以实现在一个界面看到所有内容,同时也不需要担心所显示的内容超出屏幕的大小,当超出之后可以翻阅至下一页浏览. #pragma mark - UIScrollViewDelega ...

  7. 如何在ScrollView滑动的瞬间禁用拖拽手势

    如何在ScrollView滑动的瞬间禁用拖拽手势 效果: 在UIScrollView滑动的瞬间禁用pan手势,可以防止用户按着屏幕不放后导致出现的一些莫须有的bug. // // ViewContro ...

  8. UIScrollView原理

    我是Mike Ash的Let’s Build…系列文章的忠实粉丝,在这一系列文章中他从头设计Cocoa的控件来解释他们的工作原理.在这里我要做一点类似的事情,用几行代码来实现我自己的滚动试图.不过首先 ...

  9. IOS开发之代码之九宫格

    通过UIScrollView展示图片的时候,如果直接向UIScrollView添加UIImageView,在图片数量比较少的时候是没有问题的,但是当我们添加图片数量非常多的时候,会占用大量的内存,我们 ...

随机推荐

  1. .net MVC 连接数据本地数据库三种方法

    <appSettings> <add key="webpages:Version" value="2.0.0.0" /> <add ...

  2. jQuery实用小技巧-获取选中的的下拉框和返回头部滑动动画

    //获取选中的下拉框 $('#someElement').find('option:selected'); $('#someElement option:selected'); //返回头部滑动动画 ...

  3. Basic Virus's Infection & Variation [Python]

    Learn from here Initial #!/usr/bin/python2.7 #MAGIC_STRING_skd83749872 import os import __main__ imp ...

  4. Revit如何模型导入到InfraWorks中

    Infraworks也就是以前的Autodesk Infrastructure Modeler(AIM)作为一款优秀的概念设计软件,能接收来自各种来源的数据,这篇介绍如何把revit中的建筑模型导入到 ...

  5. The specified file or folder name is too long

    You receive a "The specified file or folder name is too long" error message when you creat ...

  6. 关于Storyboard的使用

    前言:说起来码龄很久似的,但是还是有很多基础的知识都不知道,比如下面介绍的关于Stroyboard的使用.(本篇博文随笔会不断补充关于Storyboard的使用技巧,持续更新) 目录: 1.使用Str ...

  7. IOS开发之音频--录音

    前言:本篇介绍录音. 关于录音,这里提供更为详细的讲解网址:http://www.cnblogs.com/kenshincui/p/4186022.html#audioRecord  ,并且该博客有更 ...

  8. webapp设置适应pc和手机的页面宽高以及布局层叠图片文字

    <!DOCTYPE html> <html lang="zh-cn"> <head> <title>我趣旅行网-美剧迷</ti ...

  9. Images.xcassets

    Images.xcassets 概述 功能 方便用户管理图像资源. 图片获取方式 Images.xcassets中的图片资源只能通过imageNamed:方法加载,通过NSBundle的pathFor ...

  10. input 默认值为灰色,输入时清楚默认值

    input 默认值为灰色,输入时清楚默认值 <input value="please input your name" onFocus="if(value==def ...