当前位置: 首页 > news >正文

Unity笔记之下拉刷新列表

在这里插入图片描述
这样的效果;

代码:

using System;
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;public class ScrollRectUpdateView : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{private ScrollRect scrollRect;[SerializeField] private RectTransform contentTransform;[SerializeField] private ScrollRefreshInfo upRefreshInfo;[SerializeField] private ScrollRefreshInfo downRefreshInfo;private bool isUpRefresh;private bool isDownRefresh;private bool isRefreshing;[SerializeField] [ReadOnly] float refreshNumber = 100;[SerializeField] [ReadOnly] float canRefreshNumber = 50;private Action upAction;private Action downAction;private void Awake(){this.scrollRect = this.GetComponent<ScrollRect>();if (scrollRect == null) throw new NullReferenceException();upRefreshInfo.ShowAndHideSelf(false);downRefreshInfo.ShowAndHideSelf(false);this.isUpRefresh = false;this.isDownRefresh = false;isRefreshing = false;}public void OnBeginDrag(PointerEventData eventData){if (this.isRefreshing) return;}public void OnDrag(PointerEventData eventData){if (this.isRefreshing) return;var rectTransform = this.transform.GetComponent<RectTransform>();float height = 0f;var childCount = this.contentTransform.childCount;var child = this.contentTransform.GetChild(1).GetComponent<RectTransform>();if (this.contentTransform.TryGetComponent(out VerticalLayoutGroup group)){height = child.rect.height * (childCount - 2) + group.spacing * (childCount - 3) - rectTransform.rect.height;}elseheight = child.rect.height * (childCount - 2) - rectTransform.rect.height;var he = this.contentTransform.anchoredPosition.y - height;Debug.Log($"one : {he}, two : {height}");// Upif (this.contentTransform.anchoredPosition.y < 0){this.upRefreshInfo.ShowAndHideSelf(true);if (contentTransform.anchoredPosition.y - child.rect.height >= -this.canRefreshNumber){this.upRefreshInfo.SetContent("下拉可刷新");this.isUpRefresh = false;}else if (contentTransform.anchoredPosition.y - child.rect.height <= -this.refreshNumber){this.upRefreshInfo.SetContent("释放后刷新");this.isUpRefresh = true;}}else{this.isUpRefresh = false;this.upRefreshInfo.ShowAndHideSelf(false);}// downif (he > 0){this.downRefreshInfo.ShowAndHideSelf(true);if (he <= this.canRefreshNumber){this.downRefreshInfo.SetContent("上拉可刷新");this.isDownRefresh = false;}else if (he >= this.refreshNumber){this.downRefreshInfo.SetContent("释放后刷新");this.isDownRefresh = true;}}else{this.isDownRefresh = false;this.downRefreshInfo.ShowAndHideSelf(false);}}public void OnEndDrag(PointerEventData eventData){if (this.isRefreshing) return;if (this.isUpRefresh){Debug.Log("开始刷新 Up");StartCoroutine(RefreshData(this.upRefreshInfo));}else if (this.isDownRefresh){Debug.Log("开始刷新 Down");StartCoroutine(RefreshData(this.downRefreshInfo));}else{this.upRefreshInfo.ShowAndHideSelf(false);this.downRefreshInfo.ShowAndHideSelf(false);}}private IEnumerator RefreshData(ScrollRefreshInfo info){isRefreshing = true;info.SetContent("刷新中");yield return new WaitForSeconds(3);info.SetContent("刷新成功");yield return new WaitForSeconds(2);info.SetContent("释放后刷新");info.ShowAndHideSelf(false);this.isUpRefresh = false;this.isDownRefresh = false;isRefreshing = false;}
}

ScrollRefreshInfo.cs

using TMPro;
using UnityEngine;public class ScrollRefreshInfo : MonoBehaviour
{private string oldStr;[SerializeField] private GameObject imgRefresh;[SerializeField] private TMP_Text txtContent;public void SetContent(string str){if (this.oldStr == str) return;this.txtContent.text = str;this.oldStr = str;Debug.Log(str);}public void ShowAndHideSelf(bool isShow){if (this.gameObject.activeSelf != isShow)this.gameObject.SetActive(isShow);}
}

场景:
在这里插入图片描述
在这里插入图片描述
就这些了。

http://www.lryc.cn/news/339915.html

相关文章:

  • 防火墙操作!
  • 代码随想录算法训练营第四十一天| 343. 整数拆分,96.不同的二叉搜索树
  • 【MATLAB源码-第53期】m代码基于粒子群算法(PSO)的三维路径规划,显示最优路径和适应度曲线。
  • el-table多行合并
  • Vue3 + Element-Plus 使用 Table 插槽时数据未及时更新
  • vue 2 怎么把2024-04-13T17:42:19转换成短日期格式
  • 网络IO模型以及实际应用
  • 一文详解MES、ERP、SCM、WMS、APS、SCADA、PLM、QMS、CRM、EAM及其关系
  • 《Kubernetes部署篇:基于Kylin V10+ARM架构CPU使用containerd部署K8S 1.26.15集群(一主多从)》
  • maven命令
  • jetson系列开发板使用虚拟机烧录系统时,遇见无法识别开发板的情况
  • 【数据结构】树与二叉树、树与森林部分习题以及算法设计例题 2
  • Cesium之home键开关及相机位置设置
  • FreeRTOS_day1
  • Nginx日志格式化和追踪
  • 华为交换机配置telnet SSH登录步骤
  • 市面上加密混淆软件的比较和推荐
  • 最新AI创作系统ChatGPT网站源码AI绘画,GPTs,AI换脸支持,GPT联网提问、DALL-E3文生图
  • 电视盒子哪个好?2024口碑网络电视盒子排行榜
  • CookieSession
  • Nginx服务 重写功能与反向代理
  • Midjourney教程(完整版)-看这篇就够了
  • 服务器上部署GPU版的milvus向量数据库
  • 【配置】Docker安装可道云网盘
  • 复盘中得道,技术人的自由之路
  • Nginx配置大全【六大使用场景、七大负载均衡策略、四大负载健康检查】
  • GDPU Java 天码行空8
  • 《前端面试题》- JS基础 - 伪数组
  • TypeScript 基础语法
  • 服务器数据恢复—V7000存储raid5数据恢复案例