[导读]最近.....废话不多说上效果图用的是UGUI我先说思路通过判断元素的位置信息来改变Hierarchy的顺序 实现无限滚动改变位置的同时也要不断的调整Content的位置防止乱跳元素锁定就是直接锁死的元素的移动范围 当只有拖动大于一定程度时

最近.....

废话不多说上效果图

用的是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 textList = new List();

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(255, 255, 255, 255);

void Awake() {

conentRect = transform.FindChild("Content").GetComponent();

targetRec = transform.parent.FindChild("HighlightTarget").GetComponent();

}

void OnEnable() {

ItemList();

}

void Start() {

switch (_itemtype) {

case ItemType._year: InstantiateData(15, 2017); break;

case ItemType._month: InstantiateData(12, 12); break;

case ItemType._day: InstantiateData(31, 31); break;

}

itemNum = transform.FindChild("Content").childCount - 1;

contentParentHeight = conentRect.parent.GetComponent().sizeDelta.y;

conentSpacing = conentRect.GetComponent().spacing / 2;

itemHeight = textList[0].rectTransform.sizeDelta.y + conentSpacing;

if (itemNum % 2 == 0) conentLimit = (itemHeight + 5) / 2;

else conentLimit = 0;

conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentLimit);

deltaX = textList[0].GetComponent().sizeDelta.x;

deltaY = textList[0].GetComponent().sizeDelta.y;

Invoke("ItemList", 0.05f);

}

///

/// 生成子项item

///

/// 子项数量

/// 子项最大值

void InstantiateData(int itemNum, int dat) {

GameObject go;

Text testObj = conentRect.FindChild("Text").GetComponent();

for (int i = dat - itemNum + 1; i <= dat; i++) {

go = Instantiate(testObj.gameObject, conentRect);

go.GetComponent().text = i.ToString();

go.name = i.ToString();

textList.Add(go.GetComponent());

ShowItem(true);

}

Destroy(conentRect.FindChild("Text").gameObject);

}

///

/// 是增加或减少

///

///

void ShowItem(bool isIncreaseOrdecrease) {

itemHeight_min = -itemHeight;

if (_itemtype == ItemType._day) itemHeight_max = -itemHeight * itemNum - 95;

else itemHeight_max = -itemHeight * itemNum;

if (isIncreaseOrdecrease) {

foreach (Text rectItem in textList) {

if (rectItem.GetComponent().anchoredPosition.y > itemHeight_min) {

print("+");

rectItem.transform.SetSiblingIndex((int)itemNum);

}

}

print(itemHeight_min);

} else {

foreach (Text rectItem in textList) {

if (rectItem.GetComponent().anchoredPosition.y

print("-");

rectItem.transform.SetSiblingIndex(0);

}

}

print(itemHeight_max);

}

}

///

/// 渐变效果,改变大小,高亮显示

///

void ItemList() {

foreach (Text item in textList) {

float indexA = Mathf.Abs(item.GetComponent().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(0, 0, 0, 1 - indexSc_color);

item.GetComponent().localScale = new Vector3(1 - indexSc_scale, 1 - indexSc_scale * 3, 1 - indexSc_scale);

//item.GetComponent().sizeDelta = new Vector2(deltaX - (deltaX * indexSc), deltaY - (deltaY * indexSc));

}

}

///

/// 获取int类型日期,并转换为指定格式

///

///

public static string GetDateInfo() { return _year + "-" + _month + "-" + _day; }

///

/// 纠正Conent位置

///

void UpdateEx() {

if (conentRect.anchoredPosition.y > conentLimit) {

ShowItem(true);

conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentRect.anchoredPosition.y - itemHeight);

}

if (conentRect.anchoredPosition.y

ShowItem(false);

conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentRect.anchoredPosition.y + itemHeight);

}

}

///

/// 获取拖拽信息并改变Conent位置

///

///

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(0, itemHeight, 0);

ItemList();

} else {

conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentRect.anchoredPosition.y - itemHeight);

oldDragPos -= new Vector3(0, itemHeight, 0);

ItemList();

}

}

}

}

///

/// 当开始拖拽

///

///

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值

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

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

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

    效果图 用的是UGUI 我先说思路 通过判断元素的位置信息来改变Hierarchy的顺序 实现无限滚动 改变位置的同时也要不断的调整Content的位置防止乱跳 元素锁定就是直接锁死的元素的移动范围 ...

  2. Vue3+ Naive UI做一个行政区选择器

    行政区信息的获取 这里用到的行政区信息是从高德开放平台获取的,地址是: https://developer.amap.com/api/webservice/guide/api/district 如果信 ...

  3. 如何做一个一键吸空投箱的效果

    功能效果展示 运行环境 Win7,Win8,Win10 Reworld版本 体验版 vc_redist.x64 运行环境 针对零基础读者的补充 下载安装 Reworld对应版本 Reworld官网链接 ...

  4. 如何做一个超级马里奥顶方块的效果

    功能效果展示 运行环境 Win7,Win8,Win10 Reworld版本 体验版 vc_redist.x64 运行环境 针对零基础读者的补充 下载安装 Reworld对应版本 Reworld官网链接 ...

  5. 12.2 做一个读书的读书人——《逆袭大学》连载

    返回到[全文目录] 目录 12.2 做一个读书的读书人 做一个自豪的读书人 写出精彩的人生 践行终生学习 12.2 做一个读书的读书人 "万般皆下品,惟有读书高",这一句有争议的话 ...

  6. JS + CSS 做一个简易九宫格抽奖

    目录 1.CSS样式 2.JS动作 大概做出来就是这个样子,alert的弹窗样式我也没改,就默认的. 1.CSS样式 样式大概分以下方面: 创建外框及内框样式: 8个奖品小方格和中间按钮小方格: 奖品 ...

  7. css3 做一个漂亮的分割线

    原文出处: http://blog.csdn.net/majormayer/article/details/50996444 在网页中我们经常会用到分割线,但是原始的分割线并不太美观,我们可以用css ...

  8. Unity3d制作一个简单粗暴的五子棋项目工程源码

    Unity3d制作一个简单粗暴的五子棋 最终效果 项目源码 绘制棋盘 绘制构思 绘制代码 效果图 放置棋子 功能和效果 功能 效果 制作棋子 定义类和类型 棋子类型 棋盘格类 实现功能 初始棋盘格数据 ...

  9. 不要在网站上无限滚动!

    人们在浏览网站的时候是喜欢用"无限滚动",还是喜欢点击""或"查看更多"?无限滚动消除了分页的需要--分页是将数字内容分离到不同页面的过程. ...

最新文章

  1. 焦李成教授谈深度神经网络发展历程
  2. Unity3d碰撞检测始终是个问题。
  3. 二维字符数组按长度排序_字符串长度 字符数组长度
  4. sketch怎么移动图层_什么是Photoshop Express,Fix,Mix和Sketch移动应用程序?
  5. 计算机课堂听课情况记录表模板,【教师听课记录表】_教师听课评课记录表模板Word版...
  6. BZOJ.3575.[HNOI2014]道路堵塞(最短路 动态SPFA)
  7. 基于队列的锁:mcs lock简介
  8. 深入浅出Linux设备驱动编程--设备驱动中的异步通知
  9. 如何在Win10安装“Siemens TIA Openness”
  10. 5个好用的样机素材网站
  11. TQ2440之内核3.15.6移植
  12. 计算机快速格式化u盘启动,制作启动盘格式化u盘
  13. 关于移动互联网运营的分享总结
  14. 公网SSH远程连接Ubuntu【免费内网穿透】
  15. echart 广州3d_一个3D可视化项目背后的心酸:ECharts-X的坎坷路
  16. SVN 错误 Access to SVN Repository Forbidden的原因及解决方法
  17. 大型语言模型综述(二)
  18. 操作系统之文件管理(二) ※
  19. 前端练习——复仇者联盟
  20. 赛扬处理器_两款还未官宣的10代赛扬出现在海外电商平台,赛扬首次拥有4MB三级缓存...

热门文章

  1. 2017(深圳) .NET技术分享交流会 第二期,将有网络直播
  2. 分布式事务与一致性算法Paxos amp; raft amp; zab
  3. Redis集群~StackExchange.redis连接Sentinel服务器并订阅相关事件
  4. ASP.NET Core依赖注入解读amp;使用Autofac替代实现
  5. C# XML添加删除/SelectNodes/xpath
  6. goldengate mysql_使用GoldenGate实现MySQL到Oracle的数据实时同步
  7. java java 大端_Java 大小端转换
  8. js中null,undefined,false,0,'',[],{}判断方法
  9. C# 扩展object类 将string强制转换成int
  10. 【C#程序设计】教学讲义——第二章:简单C#程序设计