Unity3d—做一个年月日选择器(Scroll Rect拖动效果优化)— 无限滚动 + 锁定元素...

效果图

 

用的是UGUI

我先说思路

通过判断元素的位置信息来改变Hierarchy的顺序 实现无限滚动

改变位置的同时也要不断的调整Content的位置防止乱跳

元素锁定就是直接锁死的元素的移动范围 当只有拖动大于一定程度时才会发生改变

 

然后是面板设置

整体结构是这样子的

需要注意的是Content需要的两个组件

Content的爸爸只需要一个脚本

大小改变曲线(大致就行)

颜色渐变曲线

 

最后是脚本

  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using UnityEngine;
  5 using UnityEngine.EventSystems;
  6 using UnityEngine.UI;
  7 
  8 public class DateControl : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
  9 
 10     public enum ItemType { _year, _month, _day }
 11 
 12     public ItemType _itemtype;
 13 
 14     RectTransform conentRect;
 15 
 16     RectTransform targetRec;
 17 
 18     Vector3 oldDragPos;
 19 
 20     Vector3 newDragPos;
 21 
 22     public AnimationCurve curve_scale;//改变大小曲线
 23     public AnimationCurve curve_color;//渐变效果曲线
 24 
 25 
 26     List<Text> textList = new List<Text>();
 27 
 28     Button testBtn;
 29 
 30     float 
 31         itemHeight,             //子项item的高
 32         contentParentHeight,    //Content爸爸的高
 33         itemNum,                //子项数量
 34         itemHeight_min,         //子项最小发生改变位置
 35         itemHeight_max,         //子项最大发生改变位置
 36         conentLimit,            //Conent纠正位置
 37         conentSpacing;          //子项间隔大小
 38 
 39     float deltaX, deltaY;
 40 
 41     [HideInInspector]
 42     public static int _year, _month, _day;
 43 
 44     [HideInInspector]
 45     int dateItemNum;
 46 
 47     Color itemColor_hig = new Color32(255, 255, 255, 255);
 48 
 49     void Awake() {
 50         conentRect = transform.FindChild("Content").GetComponent<RectTransform>();
 51         targetRec = transform.parent.FindChild("HighlightTarget").GetComponent<RectTransform>();
 52 
 53     }
 54 
 55     void OnEnable() {
 56         ItemList();
 57     }
 58 
 59     void Start() {
 60         switch (_itemtype) {
 61             case ItemType._year: InstantiateData(15, 2017); break;
 62             case ItemType._month: InstantiateData(12, 12); break;
 63             case ItemType._day: InstantiateData(31, 31); break;
 64         }
 65 
 66         itemNum = transform.FindChild("Content").childCount - 1;
 67 
 68         contentParentHeight = conentRect.parent.GetComponent<RectTransform>().sizeDelta.y;
 69 
 70         conentSpacing = conentRect.GetComponent<VerticalLayoutGroup>().spacing / 2;
 71 
 72         itemHeight = textList[0].rectTransform.sizeDelta.y + conentSpacing;
 73 
 74         if (itemNum % 2 == 0) conentLimit = (itemHeight + 5) / 2;
 75 
 76         else conentLimit = 0;
 77 
 78         conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentLimit);
 79 
 80         deltaX = textList[0].GetComponent<RectTransform>().sizeDelta.x;
 81         deltaY = textList[0].GetComponent<RectTransform>().sizeDelta.y;
 82 
 83         Invoke("ItemList", 0.05f);
 84 
 85     }
 86 
 87     /// <summary>
 88     /// 生成子项item
 89     /// </summary>
 90     /// <param name="itemNum">子项数量</param>
 91     /// <param name="dat">子项最大值</param>
 92     void InstantiateData(int itemNum, int dat) {
 93         GameObject go;
 94         Text testObj = conentRect.FindChild("Text").GetComponent<Text>();
 95         for (int i = dat - itemNum + 1; i <= dat; i++) {
 96             go = Instantiate(testObj.gameObject, conentRect);
 97             go.GetComponent<Text>().text = i.ToString();
 98             go.name = i.ToString();
 99             textList.Add(go.GetComponent<Text>());
100             ShowItem(true);
101         }
102         Destroy(conentRect.FindChild("Text").gameObject);
103     }
104 
105     /// <summary>
106     /// 是增加或减少
107     /// </summary>
108     /// <param name="isIncreaseOrdecrease"></param>
109     void ShowItem(bool isIncreaseOrdecrease) {
110         itemHeight_min = -itemHeight;
111 
112         if (_itemtype == ItemType._day) itemHeight_max = -itemHeight * itemNum - 95;
113         else itemHeight_max = -itemHeight * itemNum;
114 
115         if (isIncreaseOrdecrease) {
116             foreach (Text rectItem in textList) {
117                 if (rectItem.GetComponent<RectTransform>().anchoredPosition.y > itemHeight_min) {
118                     print("+");
119                     rectItem.transform.SetSiblingIndex((int)itemNum);
120                 }
121             }
122             print(itemHeight_min);
123         } else {
124             foreach (Text rectItem in textList) {
125                 if (rectItem.GetComponent<RectTransform>().anchoredPosition.y < itemHeight_max) {
126                     print("-");
127                     rectItem.transform.SetSiblingIndex(0);
128                 }
129             }
130             print(itemHeight_max);
131 
132         }
133     }
134 
135     /// <summary>
136     /// 渐变效果,改变大小,高亮显示
137     /// </summary>
138     void ItemList() {
139         foreach (Text item in textList) {
140             float indexA = Mathf.Abs(item.GetComponent<RectTransform>().position.y - targetRec.position.y);
141             float indexSc_scale = Mathf.Abs(curve_scale.Evaluate(indexA / contentParentHeight));
142             float indexSc_color = Mathf.Abs(curve_color.Evaluate(indexA / contentParentHeight));
143             if (indexA < 15) {
144                 item.color = itemColor_hig;
145                 switch (_itemtype) {
146                     case ItemType._year: _year = int.Parse(item.text); break;
147                     case ItemType._month: _month = int.Parse(item.text); break;
148                     case ItemType._day: _day = int.Parse(item.text); break;
149                 }
150             } else item.color = new Color(0, 0, 0, 1 - indexSc_color);
151 
152             item.GetComponent<RectTransform>().localScale = new Vector3(1 - indexSc_scale, 1 - indexSc_scale * 3, 1 - indexSc_scale);
153             //item.GetComponent<RectTransform>().sizeDelta = new Vector2(deltaX - (deltaX * indexSc), deltaY - (deltaY * indexSc));
154         }
155 
156     }
157 
158     /// <summary>
159     /// 获取int类型日期,并转换为指定格式
160     /// </summary>
161     /// <returns></returns>
162     public static string GetDateInfo() { return _year + "-" + _month + "-" + _day; }
163 
164     /// <summary>
165     /// 纠正Conent位置
166     /// </summary>
167     void UpdateEx() {
168         if (conentRect.anchoredPosition.y > conentLimit) {
169             ShowItem(true);
170             conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentRect.anchoredPosition.y - itemHeight);
171         }
172         if (conentRect.anchoredPosition.y < conentLimit) {
173             ShowItem(false);
174             conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentRect.anchoredPosition.y + itemHeight);
175         }
176     }
177 
178     /// <summary>
179     /// 获取拖拽信息并改变Conent位置
180     /// </summary>
181     /// <param name="eventData"></param>
182     void SetDraggedPosition(PointerEventData eventData) {
183         if (RectTransformUtility.ScreenPointToWorldPointInRectangle(conentRect, eventData.position, eventData.pressEventCamera, out newDragPos)) {
184             newDragPos = eventData.position;
185             if (Mathf.Abs(newDragPos.y - oldDragPos.y) >= itemHeight) {
186                 if (newDragPos.y > oldDragPos.y) {
187                     conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentRect.anchoredPosition.y + itemHeight);
188                     oldDragPos += new Vector3(0, itemHeight, 0);
189                     ItemList();
190                 } else {
191                     conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentRect.anchoredPosition.y - itemHeight);
192                     oldDragPos -= new Vector3(0, itemHeight, 0);
193                     ItemList();
194                 }
195             }
196         }
197     }
198 
199     /// <summary>
200     /// 当开始拖拽
201     /// </summary>
202     /// <param name="eventData"></param>
203     public void OnBeginDrag(PointerEventData eventData) {
204         oldDragPos = eventData.position;
205     }
206 
207     public void OnDrag(PointerEventData eventData) {
208         SetDraggedPosition(eventData);
209         UpdateEx();
210     }
211 
212     public void OnEndDrag(PointerEventData eventData) {
213         SetDraggedPosition(eventData);
214         UpdateEx();
215     }
216 }

 

照着来的话基本没什么问题

因为赶时间所以很多地方写的简单粗暴请谅解

如果调整元素大小或者间隙大小 需要改变itemHeight_min 和 itemHeight_max 的值

他们分别为

itemHeight_min 

itemHeight_max 

也就是元素的最顶层和最底层的Y值

 

以上就是年月日选择器的具体步骤

 

转载于:https://www.cnblogs.com/sangblog/p/6995534.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/488148.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2020年虚拟现实和增强现实的发展趋势将会如何|0glasses低调分享

来源&#xff1a;AR工业应用2019年是虚拟现实和增强现实&#xff08;VR / AR&#xff09;增长的一年-统称为扩展现实&#xff08;XR&#xff09;。这些突破性技术的出现开始使人们远离最初流行的游戏和娱乐领域&#xff0c;用户可以戴上耳机并完全沉浸在计算机生成的环境中&…

qpaint 居中画图像_用AI画建筑分析图的好技巧。

今天给大家推送一期AI的技巧&#xff0c;有快捷键技巧&#xff0c;也有一些其他技巧&#xff0c;大家往下看&#xff0c;看有没有自己能用得到。AI技巧视图操作将图像显示为边框模式(切换) 【Ctrl】【Y】对所选对象生成预览(在边框模式中) 【Ctrl】【Shift】【Y】放大视图 【Ct…

【USACO Feb 2014】Cow Decathlon

题目描述 约翰有 N 头奶牛&#xff0c;组成了一直队伍参加全能比赛。比赛一共有 N 项&#xff0c;每头奶牛必须参加一项比赛&#xff0c;每项比赛也必须有一头奶牛参加。任何一头奶牛可以胜任任何一项比赛&#xff0c;但得分不一样。如果第i 头奶牛参加第 j 项比赛&#xff0c;…

新基建深度报告:七大领域十大龙头分析

来源&#xff1a;华泰证券稳增长、促转型&#xff0c;把握新基建浪潮中的七大产业机遇七大领域&#xff1a;5G、数据中心、云计算、工业互联网、物联网、人工智能、传 统基础设施数字化改造新基建担负着不一样的历史使命。传统基建稳需求、注重补短板&#xff0c;新基建关注新兴…

新版IDEA中Git的使用(三)

说明&#xff1a;前面介绍了在新版IDEA中Git的基本操作、分支操作&#xff0c;本文介绍一下在新版IDEA中&#xff0c;如何回滚代码&#xff1b; 分以下三个阶段来介绍&#xff1a; 未Commit的文件&#xff1b; 已经Commit&#xff0c;但未Push的文件&#xff1b; 已经Push的…

python定义二叉树_用类定义二叉树

#用类定义二叉树class BTree:def__init__(self,value):#左儿子self.left None#节点值self.data value#右儿子self.right None#向左子树插入节点definsertLeft(self,value):self.left BTree(value)returnself.left#向右子树插入节点def insertRight(self,value):self.right BTre…

html的字体红底颜色,红底黄字门头风水好吗 什么颜色招牌好

门头就是招牌&#xff0c;影响着一个店铺的生意好坏。而红底黄字的门头风水不好&#xff0c;一来是因为不容易辨识&#xff1b;二来是因为容易使人焦躁。而招牌最佳的颜色就是红底白字或白底黄字。一个店铺生意好不好&#xff0c;与自身的招牌是有着不可分割的关系的。我们除了…

python基础之名称空间和作用域、函数嵌套

一、名称空间 1、定义&#xff1a;存放名字与值的绑定关系 2、名称空间分为&#xff1a;内置名称空间、全局名称空间、局部名称空间 内置名称空间&#xff1a;python解释器自带的名字&#xff0c;python解释器启动就会生成 全局名称空间&#xff1a;文件级别定义的名字都会存放…

与微信、APP正面刚?三大运营商联合发布5G消息白皮书

来源&#xff1a;数据观综合4月8日&#xff0c;中国移动、中国电信、中国联通联合发布《5G消息白皮书》&#xff0c;宣布推出全新的信息交互服务——“5G消息”和生态建设构想&#xff0c;这意味着我国基础短信业务进入全面升级通道&#xff0c;传统短信将被5G消息(RCS业务)逐步…

Linux磁盘及文件系统(二)Linux下磁盘命名和分区

在为主机添加硬盘之前&#xff0c;首先需要了解Linux系统下对硬盘和分区的命令方法 一、磁盘命名 Linux下对SCSI和SATA设备是以sd命名的&#xff0c;第一个SCSI设备是sda,第二个是sdb....以此类推。一般主板上有两个SCSI接口&#xff0c;因此一共可以安装4个SCSI设备。主SCSI上…

tensorflow如何取平均_【他山之石】利用Tensorflow构建CNN图像多分类模型及图像参数、数据维度变化情况实例分析...

“他山之石&#xff0c;可以攻玉”&#xff0c;站在巨人的肩膀才能看得更高&#xff0c;走得更远。在科研的道路上&#xff0c;更需借助东风才能更快前行。为此&#xff0c;我们特别搜集整理了一些实用的代码链接&#xff0c;数据集&#xff0c;软件&#xff0c;编程技巧等&…

计算机涉及数学知识点,初二数学知识点归纳

无论你是带着欣喜还是遗憾进入了初二&#xff0c;一切都在这一刻重新开始。初二的数学学习开始啦!大家有没有什么好的学习方法来分享呢?知识点要学得明白&#xff0c;记得牢固。初二数学上册的知识点你知道吗?不知道的赶紧和沪江小编一起往下看吧。1 全等三角形的对应边、对应…

华为AR地图正式发布:每平方公里40亿三维信息点,1:1还原世界

来源&#xff1a;M数码4月8日晚&#xff0c;华为P40系列发布会上&#xff0c;余承东介绍了全新的华为AR地图&#xff0c;官方称实现了每平方公里40亿三维信息点&#xff0c;1:1还原真实世界。官方称华为AR地图是厘米级3D地图&#xff0c;每平方公里40亿三维信息点&#xff0c;1…

4_用户管理

用户怎么管理&#xff1f; -- 创建用户 - create user ‘用户名’’ ip地址’ indentified by ‘密码’; -- 删除用户 - drop user ‘用户名’’ ip地址’; -- 修改用户 -rename user ‘用户名’’ ip地址’ to ‘新用户’’ ip地址’; -- 修改密码 - set password for ‘用户…

船员能力评估计算机软件系统,船舶动力电力训练系统操作水平综合评估算法研究...

摘要&#xff1a;近年来随着航运事业的不断发展,航海专业技术人员的需求量持续增大,培养大量高质量的船员对于保证船舶航行的安全性和经济性具有重要意义。传统的实船操作训练评估方法受到资源有限、安全性较低以及人力评估的主观性等问题的影响,难以满足船舶对于大量优质船员的…

《全球创新观察》研究报告3月刊下发布

来源&#xff1a; 资本实验室大疫之下&#xff0c;全球产业格局有哪些变动&#xff1f;5G竞赛&#xff0c;又为哪些行业带来新风口&#xff1f;一起来看本期《全球创新观察》▼关于《全球创新观察》本报告是由国创会创新院&#xff08;中国科学院国家创新与发展战略研究会创新驱…

python123字典统计排序1省份_python 列表、字典多排序问题

版权声明&#xff1a;本文为博主原创文章&#xff0c;遵循 CC 4.0 by-sa 版权协议&#xff0c;转载请附上原文出处链接和本声明。本文链接&#xff1a;https://blog.csdn.net/justin051/article/details/84289189Python使用sorted函数来排序&#xff1a;l [2,1,3,5,7,3]print …

rem和css3的相关知识点

☆☆☆rem和css3的相关知识点☆☆☆ 一、 Web front-end development engineer rem是根据页面的根元素的font-size的一个相对的单位&#xff0c;即 html{ font-size:16px;//自定义根部元素的字体大小 } 比如我们在一个div中&#xff0c;如此写 div{ width:2rem; }那么我们的wid…

青岛农商银行计算机防病毒应用培训,青岛农商银行胶州支行多元化培训提升安防管理水平...

今年以来&#xff0c;青岛农商银行胶州支行面对当前严峻的社会治安形势&#xff0c;不等不靠&#xff0c;积极采取多元化、立体式培训模式&#xff0c;取得较好效果&#xff0c;有效提升了安防管理水平。请进来&#xff0c;抓好安保理念教育近期&#xff0c;该支行积极协调胶州…

简述div标签和span标签的不同_div与span区别及用法

DIV与SPAN区别及div与san用法篇接下来了解在divcss开发的时候在html网页制作&#xff0c;特别是标签运用中div和span的区别及用法。新手在使用web标准(div css)开发网页的时候&#xff0c;遇到第一个问题是div与span有什么区别&#xff0c;什么时候用div&#xff0c;什么时候用…