吸附式滚动列表

2024-01-05 21:08
文章标签 列表 滚动 吸附

本文主要是介绍吸附式滚动列表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;/// <summary>
/// 吸附式滚动列表
/// </summary>
public class PageAdsorbScrollview : MonoBehaviour, IEndDragHandler, IBeginDragHandler
{public enum ScrollType{Horizontal,Vertical}private ScrollRect scrollRect;private RectTransform content;private GridLayoutGroup gridLayoutGroup;private int pageCount;private float[] pagesData; //位置private float timer = 0; //吸附计时器private float startMovePos; //开始移动的位置private Vector2 startMovePosV2; //开始移动的位置private int currentIndex = 0; //当前所处页数private bool isMoving = false; //是否正在吸附中private bool isDraging = false; //是否在拖动中private Vector2 beginDragPointerPos; //开始拖动时的位置private float autotimer = 0; //自动计时器private float finalPosOffsetBackup = 0f;private Vector2 originalCellSize;[Header("滑动类型")]public ScrollType scrollType = ScrollType.Vertical;[Header("吸附速度"), Range(0f, 50f)]public float moveSpeed = 5f;[Header("终点位置偏移(0为最终吸附到顶部)")]public float finalPosOffset = 0f;[Header("界限偏移(0为五五分判定,>0更容易拖动,<0更难拖动)")]public float distanceOffset = 0f;[Header("是否自动滚动")]public bool isAutoScroll = true; //是否自动滚动[Header("自动滚动时间间隔"), Range(0f, 60f)]public float AutoTime = 1f; //自动滚动时间间隔private void Awake(){scrollRect = GetComponent<ScrollRect>();if (scrollRect == null){Debug.LogError("不存在组件ScrollRect");}else{content = scrollRect.content;if (content == null){Debug.LogError("不存在节点ScrollRect->Content");}else{gridLayoutGroup = content.GetComponent<GridLayoutGroup>();if (gridLayoutGroup == null){Debug.LogError("不存在组件ScrollRect->Content->GridLayoutGroup");}originalCellSize = gridLayoutGroup.cellSize;}}finalPosOffsetBackup = finalPosOffset;}private void Start(){Init();}private void Update(){CheckMove();CheckAutoScroll();if (finalPosOffsetBackup != finalPosOffset){finalPosOffsetBackup = finalPosOffset;Init();ScrollTo(currentIndex);}}private void Init(){pageCount = content.childCount; //子节点数量pagesData = new float[pageCount]; //初始化for (int i = 0; i < pagesData.Length; i++){switch (scrollType){case ScrollType.Horizontal:pagesData[i] = i * (1f / (float) (pageCount - 1)); //给位置的数组赋值break;case ScrollType.Vertical:pagesData[i] = CalculateTopPosition(i);break;}//DELETEMEif (content.GetChild(i).Find("Text") != null)content.GetChild(i).Find("Text").GetComponent<Text>().text = pagesData[i].ToString();}}private float CalculateTopPosition(int childIndex){if (childIndex <= 0) return 0f  +  finalPosOffset;return gridLayoutGroup.padding.top + childIndex * originalCellSize.y + (childIndex - 1) * gridLayoutGroup.spacing.y  +  finalPosOffset;}private void CheckMove(){if (isMoving){timer += Time.unscaledDeltaTime * moveSpeed;switch (scrollType){case ScrollType.Horizontal:scrollRect.horizontalNormalizedPosition = Mathf.Lerp(startMovePos, pagesData[currentIndex], timer);break;case ScrollType.Vertical:content.anchoredPosition = Vector2.Lerp(startMovePosV2, new Vector2(startMovePosV2.x, pagesData[currentIndex]), timer);break;}if (timer > 1){isMoving = false;scrollRect.StopMovement();}}else{scrollRect.StopMovement();}}private void CheckAutoScroll() //自动滑动{if (isDraging){return;}if (isAutoScroll){autotimer += Time.deltaTime;if (autotimer > AutoTime){autotimer = 0;currentIndex++;currentIndex %= pageCount; //取余形成循环ScrollTo(currentIndex);}}}private void ScrollTo(int index){currentIndex = index;timer = 0;isMoving = true;switch (scrollType){case ScrollType.Horizontal:startMovePos = scrollRect.horizontalNormalizedPosition;break;case ScrollType.Vertical:startMovePosV2 = content.anchoredPosition;break;}}public void OnBeginDrag(PointerEventData eventData){isDraging = true;beginDragPointerPos = eventData.position;}public void OnEndDrag(PointerEventData eventData){int minIndex = 0;//计算当最近的一页是哪个switch (scrollType){case ScrollType.Horizontal:for (int i = 0; i < pagesData.Length; i++){if (Mathf.Abs(pagesData[i] - scrollRect.horizontalNormalizedPosition) < Mathf.Abs(pagesData[minIndex] - scrollRect.horizontalNormalizedPosition)){minIndex = i;}}break;case ScrollType.Vertical:var curY = content.anchoredPosition.y;var directionY = Mathf.Sign(eventData.position.y - beginDragPointerPos.y);for (int i = 0; i < pagesData.Length; i++){if (Mathf.Abs(pagesData[i] - curY) - directionY * distanceOffset < Mathf.Abs(pagesData[minIndex] - curY) + directionY * distanceOffset){minIndex = i;}}break;}ScrollTo(minIndex);isDraging = false;autotimer = 0;}
}

备注:Vertical可用,Horizontal未写完,若使用后者请修改相应switch语句。

这篇关于吸附式滚动列表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/574167

相关文章

Python中DataFrame转列表的最全指南

《Python中DataFrame转列表的最全指南》在Python数据分析中,Pandas的DataFrame是最常用的数据结构之一,本文将为你详解5种主流DataFrame转换为列表的方法,大家可以... 目录引言一、基础转换方法解析1. tolist()直接转换法2. values.tolist()矩阵

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

python展开嵌套列表的多种方法

《python展开嵌套列表的多种方法》本文主要介绍了python展开嵌套列表的多种方法,包括for循环、列表推导式和sum函数三种方法,具有一定的参考价值,感兴趣的可以了解一下... 目录一、嵌套列表格式二、嵌套列表展开方法(一)for循环(1)for循环+append()(2)for循环+pyPhWiFd

Python容器类型之列表/字典/元组/集合方式

《Python容器类型之列表/字典/元组/集合方式》:本文主要介绍Python容器类型之列表/字典/元组/集合方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 列表(List) - 有序可变序列1.1 基本特性1.2 核心操作1.3 应用场景2. 字典(D

Java中数组转换为列表的两种实现方式(超简单)

《Java中数组转换为列表的两种实现方式(超简单)》本文介绍了在Java中将数组转换为列表的两种常见方法使用Arrays.asList和Java8的StreamAPI,Arrays.asList方法简... 目录1. 使用Java Collections框架(Arrays.asList)1.1 示例代码1.

python中列表list切分的实现

《python中列表list切分的实现》列表是Python中最常用的数据结构之一,经常需要对列表进行切分操作,本文主要介绍了python中列表list切分的实现,文中通过示例代码介绍的非常详细,对大家... 目录一、列表切片的基本用法1.1 基本切片操作1.2 切片的负索引1.3 切片的省略二、列表切分的高

禁止HTML页面滚动的操作方法

《禁止HTML页面滚动的操作方法》:本文主要介绍了三种禁止HTML页面滚动的方法:通过CSS的overflow属性、使用JavaScript的滚动事件监听器以及使用CSS的position:fixed属性,每种方法都有其适用场景和优缺点,详细内容请阅读本文,希望能对你有所帮助... 在前端开发中,禁止htm

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

Redis存储的列表分页和检索的实现方法

《Redis存储的列表分页和检索的实现方法》在Redis中,列表(List)是一种有序的数据结构,通常用于存储一系列元素,由于列表是有序的,可以通过索引来访问元素,因此可以很方便地实现分页和检索功能,... 目录一、Redis 列表的基本操作二、分页实现三、检索实现3.1 方法 1:客户端过滤3.2 方法

Python实现将实体类列表数据导出到Excel文件

《Python实现将实体类列表数据导出到Excel文件》在数据处理和报告生成中,将实体类的列表数据导出到Excel文件是一项常见任务,Python提供了多种库来实现这一目标,下面就来跟随小编一起学习一... 目录一、环境准备二、定义实体类三、创建实体类列表四、将实体类列表转换为DataFrame五、导出Da