吸附式滚动列表

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中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

html 滚动条滚动过快会留下边框线的解决方案

《html滚动条滚动过快会留下边框线的解决方案》:本文主要介绍了html滚动条滚动过快会留下边框线的解决方案,解决方法很简单,详细内容请阅读本文,希望能对你有所帮助... 滚动条滚动过快时,会留下边框线但其实大部分时候是这样的,没有多出边框线的滚动条滚动过快时留下边框线的问题通常与滚动条样式和滚动行

Python中合并列表(list)的六种方法小结

《Python中合并列表(list)的六种方法小结》本文主要介绍了Python中合并列表(list)的六种方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录一、直接用 + 合并列表二、用 extend() js方法三、用 zip() 函数交叉合并四、用

Spring Boot中的YML配置列表及应用小结

《SpringBoot中的YML配置列表及应用小结》在SpringBoot中使用YAML进行列表的配置不仅简洁明了,还能提高代码的可读性和可维护性,:本文主要介绍SpringBoot中的YML配... 目录YAML列表的基础语法在Spring Boot中的应用从YAML读取列表列表中的复杂对象其他注意事项总

uniapp小程序中实现无缝衔接滚动效果代码示例

《uniapp小程序中实现无缝衔接滚动效果代码示例》:本文主要介绍uniapp小程序中实现无缝衔接滚动效果的相关资料,该方法可以实现滚动内容中字的不同的颜色更改,并且可以根据需要进行艺术化更改和自... 组件滚动通知只能实现简单的滚动效果,不能实现滚动内容中的字进行不同颜色的更改,下面实现一个无缝衔接的滚动

C++类和对象之初始化列表的使用方式

《C++类和对象之初始化列表的使用方式》:本文主要介绍C++类和对象之初始化列表的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C++初始化列表详解:性能优化与正确实践什么是初始化列表?初始化列表的三大核心作用1. 性能优化:避免不必要的赋值操作2. 强

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

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