Unity笔记之下拉刷新列表

news2024/7/30 4:19:16

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

代码:

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;
        }
        else
            height = child.rect.height * (childCount - 2) - rectTransform.rect.height;

        var he = this.contentTransform.anchoredPosition.y - height;
        Debug.Log($"one : {he}, two : {height}");

        // Up
        if (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);
        }

        // down
        if (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.coloradmin.cn/o/1597759.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Three.js--》穿越虚拟门槛打造的3D登录界面

今天简单实现一个three.js的小Demo&#xff0c;加强自己对three知识的掌握与学习&#xff0c;只有在项目中才能灵活将所学知识运用起来&#xff0c;话不多说直接开始。 目录 项目搭建 初始化three代码 添加背景与地球 星星动画效果 星云动画效果 实现登录框效果 项目搭建…

C语言——内存函数的实现和模拟实现

1. memcpy 使用和模拟实现 void * memcpy ( void * destination, const void * source, size_t num ); 函数memcpy从source的位置开始向后复制num个字节的数据到destination指向的内存位置。 这个函数在遇到 \0 的时候并不会停下来。 如果source和destination有任何的重叠&am…

高级数据结构与算法习题(7)

一、单选题 1、When solving a problem with input size N by divide and conquer, if at each stage the problem is divided into 8 sub-problems of equal size N/3, and the conquer step takes O(N2logN) to form the solution from the sub-solutions, then the overall…

【Docker系列】容器访问宿主机的Mysql

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学…

flutter material中的Icon组件的IconData 查阅

查阅 https://fonts.google.com/icons?selectedMaterialSymbolsOutlined:expand_less:FILL0;wght300;GRAD0;opsz24&icon.platformandroidhttps://fonts.google.com/icons?selectedMaterialSymbolsOutlined:expand_less:FILL0;wght300;GRAD0;opsz24&icon.platformand…

mysql报错-mysql服务启动停止后,某些服务在未由其他服务或程序使用时将自动停止和数据恢复

启动mysql服务时出现该错误: 本地计算机上的mysql服务启动停止后,某些服务在未由其他服务或程序使用时将自动停止。 我的mysql版本是8.0.18 系统&#xff1a;win10 如何安装mysql&#xff0c;可以看我这一篇文章&#xff1a;mysql的安装 ---必会 - bigbigbrid - 博客园 (cn…

2024 EasyRecovery三分钟帮你恢复 电脑硬盘格式化

随着数字化时代的到来&#xff0c;我们的生活和工作中越来越依赖于电子设备。然而&#xff0c;电子设备中的数据丢失问题也随之而来。数据丢失可能是由各种原因引起的&#xff0c;如硬盘故障、病毒感染、误删除等。面对这种情况&#xff0c;一个高效、可靠的数据恢复工具变得尤…

【御控物联】Java JSON结构转换(1):对象To对象——键值互换

文章目录 一、JSON是什么&#xff1f;二、JSON结构转换是什么&#xff1f;三、核心构件之转换映射四、案例之《JSON对象 To JSON对象》五、代码实现六、在线转换工具七、技术资料 一、JSON是什么&#xff1f; Json&#xff08;JavaScript Object Notation&#xff09;产生于20…

SkyWalking 为所有的API接口增加 tag

背景胡扯 线上接口报错&#xff0c;接着被 SkyWalking 抓到&#xff0c;然后 SkyWalking 触发告警&#xff0c;最后老板你&#xff0c;让你辛苦一下&#xff0c;在明早上班前把这个bug 改了&#xff0c;并告诉你你是全公司的希望。谁说不是呢&#xff1f;为公司业务保驾护航&a…

Opencv3.4+FFMpeg3.4+pkg-config交叉编译arm开发板

Ubuntu16.04 64位 FFmpeg3.4 OpenCv3.4 一、下载FFmpeg https://github.com/FFmpeg/FFmpeg 1.配置 ./configure --prefix/home/zeng/ffmpeg_install --enable-cross-compile --cross-prefixarm-linux-gnueabihf- --ccarm-linux-gnueabihf-gcc --target-oslinux --cpuco…

nginx的启动,systemd管理

service unit file文件通常由三部分组成&#xff1a; [Unit]&#xff1a;定义与Unit类型无关的通用选项&#xff1b;用于提供unit的描述信息、unit行为及依赖关系等 [Service]&#xff1a;与特定类型相关的专用选项&#xff1b;此处为Service类型 [Install]&#xff1a;定义…

CTFshow-PWN-前置基础(pwn6-pwn9)

1、pwn6 立即寻址方式结束后eax寄存器的值为&#xff1f; 截取关键代码 ; 立即寻址方式mov eax, 11 ; 将11赋值给eaxadd eax, 114504 ; eax加上114504sub eax, 1 ; eax减去1 mov 用于赋值&#xff0c;将右边立即数加载到左边的寄存器 eax 中&#xff1b…

电商的未来:回归传统的力量

引言 随着拼多多的崛起&#xff0c;淘宝的市场地位受到了挑战。马云在阿里巴巴内网发表的《致改革 致创新》一文中&#xff0c;呼吁直面问题与未来&#xff0c;但具体的改革与创新方向尚未明晰。本文将探讨电商的未来&#xff0c;提出一个观点&#xff1a;电商的真正未来不在于…

网络安全-自学笔记

一、自学网络安全学习的误区和陷阱 1.不要试图先成为一名程序员&#xff08;以编程为基础的学习&#xff09;再开始学习 我在之前的回答中&#xff0c;我都一再强调不要以编程为基础再开始学习网络安全&#xff0c;一般来说&#xff0c;学习编程不但学习周期长&#xff0c;而…

leaflet知识点:地图窗格panes的应用

一&#xff0c;需求背景 地图中存在无人机&#xff0c;停机坪&#xff0c;航线三个图层&#xff0c;需要实现无人机图层显示在最上面&#xff0c;停机坪图层显示在最下面&#xff0c;航线图层显示在中间。 二&#xff0c;遇到问题 由下图可知航线图层所在overlayPane窗格的z-…

基于SSM项目高校在线请假与审批系统

采用技术 基于SpringBoot框架实现的web的智慧社区系统的设计与实现~ 开发语言&#xff1a;Java 数据库&#xff1a;MySQL 技术&#xff1a;SpringMVCMyBatis 工具&#xff1a;IDEA/Ecilpse、Navicat、Maven 简介 本系统实现了管理员&#xff0c;教师&#xff0c;学生三个模…

OpenHarmony开发实例:【分布式游戏鉴权应用】

1.介绍 本文将介绍分布式游戏鉴权应用。操作过程为&#xff1a; 设备A点击“开始游戏”按钮&#xff0c;开始搜索周边设备。 设备A显示周边设备&#xff0c;点击设备B并发起连接请求&#xff0c;远程拉起设备B的FA。 设备B收到请求后&#xff0c;选择是否允许“开启游戏”。…

【大语言模型】基础:TF-IDF

TF-IDF (Term Frequency-Inverse Document Frequency) 是一种用于信息检索与文本挖掘的统计方法&#xff0c;用来评估一个词对于一个文件集或一个语料库中的其中一份文件的重要性。它是一种常用于文本处理和自然语言处理的权重计算技术。 原理 TF-IDF 由两部分组成&#xff1…

获取淘宝京东商品详情API接口返回数据解析说明(可测试,批量获取)

获取淘宝和京东的商品详情API接口返回数据并解析通常需要遵循以下几个步骤&#xff1a; 淘宝商品详情API 淘宝的API接口通常对商家和合作伙伴开放&#xff0c;并且需要经过严格的申请和审核流程。普通用户或未经授权的开发者通常无法直接访问淘宝的商品详情API。 如果你已经…

广东莱斯广告,6.8米UV喷印推动粤东喷绘产业升级

广东莱斯广告作为汕头市大型的广告服务运营商,近日迎来了一件值得庆祝的事情:彩神6.8米UV喷印机运行一周年,销售服务商深圳嘉豪总经理李伟特地前来回访。该设备是深圳润天智数字设备股份有限公司开发的全球首台搭载XTRA6800H柯尼卡喷头的设备,设备特点是:1.色彩艳丽;2.超宽喷印…