效果图

用的是UGUI

我先说思路

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

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

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

然后是面板设置

整体结构是这样子的

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

Content的爸爸只需要一个脚本

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

颜色渐变曲线

最后是脚本

 using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI; public class DateControl : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public enum ItemType { _year, _month, _day } public ItemType _itemtype; RectTransform conentRect; RectTransform targetRec; Vector3 oldDragPos; Vector3 newDragPos; public AnimationCurve curve_scale;//改变大小曲线
public AnimationCurve curve_color;//渐变效果曲线 List<Text> textList = new List<Text>(); Button testBtn; float
itemHeight, //子项item的高
contentParentHeight, //Content爸爸的高
itemNum, //子项数量
itemHeight_min, //子项最小发生改变位置
itemHeight_max, //子项最大发生改变位置
conentLimit, //Conent纠正位置
conentSpacing; //子项间隔大小 float deltaX, deltaY; [HideInInspector]
public static int _year, _month, _day; [HideInInspector]
int dateItemNum; Color itemColor_hig = new Color32(, , , ); void Awake() {
conentRect = transform.FindChild("Content").GetComponent<RectTransform>();
targetRec = transform.parent.FindChild("HighlightTarget").GetComponent<RectTransform>(); } void OnEnable() {
ItemList();
} void Start() {
switch (_itemtype) {
case ItemType._year: InstantiateData(, ); break;
case ItemType._month: InstantiateData(, ); break;
case ItemType._day: InstantiateData(, ); break;
} itemNum = transform.FindChild("Content").childCount - ; contentParentHeight = conentRect.parent.GetComponent<RectTransform>().sizeDelta.y; conentSpacing = conentRect.GetComponent<VerticalLayoutGroup>().spacing / ; itemHeight = textList[].rectTransform.sizeDelta.y + conentSpacing; if (itemNum % == ) conentLimit = (itemHeight + ) / ; else conentLimit = ; conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentLimit); deltaX = textList[].GetComponent<RectTransform>().sizeDelta.x;
deltaY = textList[].GetComponent<RectTransform>().sizeDelta.y; Invoke("ItemList", 0.05f); } /// <summary>
/// 生成子项item
/// </summary>
/// <param name="itemNum">子项数量</param>
/// <param name="dat">子项最大值</param>
void InstantiateData(int itemNum, int dat) {
GameObject go;
Text testObj = conentRect.FindChild("Text").GetComponent<Text>();
for (int i = dat - itemNum + ; i <= dat; i++) {
go = Instantiate(testObj.gameObject, conentRect);
go.GetComponent<Text>().text = i.ToString();
go.name = i.ToString();
textList.Add(go.GetComponent<Text>());
ShowItem(true);
}
Destroy(conentRect.FindChild("Text").gameObject);
} /// <summary>
/// 是增加或减少
/// </summary>
/// <param name="isIncreaseOrdecrease"></param>
void ShowItem(bool isIncreaseOrdecrease) {
itemHeight_min = -itemHeight; if (_itemtype == ItemType._day) itemHeight_max = -itemHeight * itemNum - ;
else itemHeight_max = -itemHeight * itemNum; if (isIncreaseOrdecrease) {
foreach (Text rectItem in textList) {
if (rectItem.GetComponent<RectTransform>().anchoredPosition.y > itemHeight_min) {
print("+");
rectItem.transform.SetSiblingIndex((int)itemNum);
}
}
print(itemHeight_min);
} else {
foreach (Text rectItem in textList) {
if (rectItem.GetComponent<RectTransform>().anchoredPosition.y < itemHeight_max) {
print("-");
rectItem.transform.SetSiblingIndex();
}
}
print(itemHeight_max); }
} /// <summary>
/// 渐变效果,改变大小,高亮显示
/// </summary>
void ItemList() {
foreach (Text item in textList) {
float indexA = Mathf.Abs(item.GetComponent<RectTransform>().position.y - targetRec.position.y);
float indexSc_scale = Mathf.Abs(curve_scale.Evaluate(indexA / contentParentHeight));
float indexSc_color = Mathf.Abs(curve_color.Evaluate(indexA / contentParentHeight));
if (indexA < ) {
item.color = itemColor_hig;
switch (_itemtype) {
case ItemType._year: _year = int.Parse(item.text); break;
case ItemType._month: _month = int.Parse(item.text); break;
case ItemType._day: _day = int.Parse(item.text); break;
}
} else item.color = new Color(, , , - indexSc_color); item.GetComponent<RectTransform>().localScale = new Vector3( - indexSc_scale, - indexSc_scale * , - indexSc_scale);
//item.GetComponent<RectTransform>().sizeDelta = new Vector2(deltaX - (deltaX * indexSc), deltaY - (deltaY * indexSc));
} } /// <summary>
/// 获取int类型日期,并转换为指定格式
/// </summary>
/// <returns></returns>
public static string GetDateInfo() { return _year + "-" + _month + "-" + _day; } /// <summary>
/// 纠正Conent位置
/// </summary>
void UpdateEx() {
if (conentRect.anchoredPosition.y > conentLimit) {
ShowItem(true);
conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentRect.anchoredPosition.y - itemHeight);
}
if (conentRect.anchoredPosition.y < conentLimit) {
ShowItem(false);
conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentRect.anchoredPosition.y + itemHeight);
}
} /// <summary>
/// 获取拖拽信息并改变Conent位置
/// </summary>
/// <param name="eventData"></param>
void SetDraggedPosition(PointerEventData eventData) {
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(conentRect, eventData.position, eventData.pressEventCamera, out newDragPos)) {
newDragPos = eventData.position;
if (Mathf.Abs(newDragPos.y - oldDragPos.y) >= itemHeight) {
if (newDragPos.y > oldDragPos.y) {
conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentRect.anchoredPosition.y + itemHeight);
oldDragPos += new Vector3(, itemHeight, );
ItemList();
} else {
conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentRect.anchoredPosition.y - itemHeight);
oldDragPos -= new Vector3(, itemHeight, );
ItemList();
}
}
}
} /// <summary>
/// 当开始拖拽
/// </summary>
/// <param name="eventData"></param>
public void OnBeginDrag(PointerEventData eventData) {
oldDragPos = eventData.position;
} public void OnDrag(PointerEventData eventData) {
SetDraggedPosition(eventData);
UpdateEx();
} public void OnEndDrag(PointerEventData eventData) {
SetDraggedPosition(eventData);
UpdateEx();
}
}

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

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

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

他们分别为

itemHeight_min

itemHeight_max 

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

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

Unity3d—做一个年月日选择器(Scroll Rect拖动效果优化)— 无限滚动 + 锁定元素的更多相关文章

  1. 用Django做一个省份选择器

    做一个省份选择器 使用django做后端, mysql数据库, jQuery 列出结构主要的文件, 其它配置比较简单 models.py 因为所有数据的结构基本一致, 把所有省份, 市和区全部存储一张 ...

  2. 关于longPressGesture做一个长按连加的效果(原创)

    关于longPressGesture做一个长按连加的效果 解释一下什么意思呢?就是一个button长按之后其数字的一直累加.朋友们可能看起来很简单,无非就是加一个长按手势(longPressGestu ...

  3. WPF 自己做一个颜色选择器

    程序开发过程中,经常会遇到需要支持动态配置主题颜色的问题,通常,一个程序会有多种不同的颜色风格主题供选 有时候,更细致一些的地方,会需要支持自己配置颜色,这样我们就需要一个颜色选择器啦,下面是我自己开 ...

  4. 做一个360度看车的效果玩玩(web)

    前几天在 Lexus 官网看到有这样的一个效果:http://www.lexus.com.cn/models/es/360 于是顺手打开控制台看了下他们是怎么做的,发现使用的技术还是比较简单的,通过背 ...

  5. css3加js做一个简单的3D行星运转效果

    前几天在园子里看到一篇关于CSS3D行星运转的文章,原文在这里,感觉这个效果也太酷炫了,于是自己也就心血来潮的来尝试的做了一下.因为懒得去用什么插件了,于是就原生的JS写,效果有点粗超,还有一些地方处 ...

  6. 做一个阅读管理APP

    背景 由于最近在看的书有点多,所以一直想找一个能够管理阅读进度的书(鄙人记性不是很好,两天不看就忘了)可惜Android平台上一直找不到合适的APP: 有没有读书进度管理的网站或软件啊? 有没有记录读 ...

  7. Infinite Scroll - jQuery & WP 无限滚动插件

    无限滚动(Infinite Scroll)也称为自动分页.滚动分页和无限分页.常用在图片.文章或其它列表形式的网页中,用来在滚动网页的时候自动加载下一页的内容.Infinite Scroll  这款  ...

  8. 用Jquery做一个时间日期选择器

    今天我们就用Jquery做一个时间日期选择器,当打开网页时,文本框里面显示的是当前的日期,点击文本框可以出现年.月.日的下拉菜单,并且可以选择,会根据年份的选择判断是否是闰年,从而改变二月的天数,闰年 ...

  9. 【跟我一起学Unity3D】做一个2D的90坦克大战之AI系统

    对于AI,我的初始想法非常easy,首先他要能动,而且是在地图里面动. 懂得撞墙后转弯,然后懂得射击,其它的没有了,基于这个想法,我首先创建了一个MyTank类,用于管理玩家的坦克的活动,然后创建AI ...

随机推荐

  1. Array和ArrayCollection作为数据源的一个应用区别

    在不用[Enabled]元标签的前提下,将一个Array赋值给DataGrid.DataList等控件的DataProvider后,当Array值发生改变时,控件显示内容不会及时更新(可调用控件的in ...

  2. DirectFB 之 FillRectangle 绘制矩形

    1. 函数原型解析 函数声明: DFBResult FillRectangle (     IDirectFBSurface    *  thiz,      int     x,      int ...

  3. Installing MySQL on Microsoft Windows Using a noinstall Zip Archive

    这两天在自己的windows上安装了一下mySql数据库,安装使用的是5.7.18版本的 noinstall Zip Archive安装包mysql-5.7.18-win32.zip.由于5.7版本相 ...

  4. Java:构造器,构造代码块,静态代码块的执行顺序

    1.构造器:与类同名且没有返回值,用来初始化类属性: 构造器又分为无参构造器和有参构造器 1.1:无参构造器 public class Contruction{ ...属性... public Con ...

  5. Python错误集

    1-->IndentationError:expected an indented block   >IndentationError: unindent does not match a ...

  6. Oracle的substr函数

    一.Substr函数 substr(目标字符串,开始位置,长度) 注意:这里第三个参数:长度,相当于物理中的标量,没有方向性,所以不能用负值.虽然不报错,但是选择不出任何值出来(欢迎指正) 开始位置可 ...

  7. Ajax理解总结

    前端开发拿数据页面实时更新是离不开Ajax这个技术的 AJAX即"Asynchronous Javascript And XML"(异步JavaScript和XML),是指一种创建 ...

  8. jQuery遍历节点方法汇总

    1.children()方法:$('div').children()---遍历查找div元素的所有子元素节点 <p>Hello</p> <div> <span ...

  9. 3.从AbstractQueuedSynchronizer(AQS)说起(2)——共享模式的锁获取与释放

    在上节中解析了AbstractQueuedSynchronizer(AQS)中独占模式对同步状态获取和释放的实现过程.本节将会对共享模式的同步状态获取和释放过程做一个解析.上一节提到了独占模式和共享模 ...

  10. Python爬虫一:爬取上交所上市公司信息

    前几天领导让写一个从新闻语料中识别上市公司的方案.上市公司属于组织机构的范畴,组织机构识别属于命名实体识别的范畴.命名实体识别包括人名.地名.组织机构等信息的识别. 要想从新闻语料中识别上市公司就需要 ...