找回密码
 立即注册
查看: 422|回复: 0

Unity UGUI实现滑动翻页效果

[复制链接]
发表于 2021-11-16 19:36 | 显示全部楼层 |阅读模式
参考连接:https://blog.csdn.net/weixin_41814169/article/details/88890650


今天做一个滑动翻页效果,便用上面连接的轮子做滑动翻页效果,但是这个滑动翻页效果只支持滑动,不支持设置index之后滑动过去,故做了修改以支持idnex滑动翻页效果~
顺便记录一下~
注意:


SetIndexMove(int index) 这个方法便是设置index滑动
using UnityEngine;using System.Collections;using UnityEngine.UI;using System.Collections.Generic;using UnityEngine.EventSystems;using System;    public class PageView : MonoBehaviour, IBeginDragHandler, IEndDragHandler    {        ScrollRect rect;                        //滑动组件                                                  //public ScrollRect rect2;                        //滑动组件2          private float targethorizontal = 0;             //滑动的起始坐标          private bool isDrag = false;                    //是否拖拽结束          private List<float> posList = new List<float>();//求出每页的临界角,页索引从0开始          private int currentPageIndex = -1;        public Action<int> OnPageChanged;        private bool stopMove = true;        public float smooting = 4;      //滑动速度          public float sensitivity = 0;        private float startTime;        private float startDragHorizontal;        bool isInit = false;        public void Init()        {            // rect = rect2;            rect = transform.GetComponent<ScrollRect>();            // rect2 = transform.GetComponent<ScrollRect>();            float horizontalLength = rect.content.rect.width - GetComponent<RectTransform>().rect.width;            //float horizontalLength2 = rect2.content.rect.width - GetComponent<RectTransform>().rect.width;            posList.Add(0);            for (int i = 1; i < rect.content.transform.childCount - 1; i++)            {                posList.Add(GetComponent<RectTransform>().rect.width * i / horizontalLength);            }            posList.Add(1);            isInit = true;        }        void Update()        {            if (isInit)            {                if (!isDrag && !stopMove)                {                    startTime += Time.deltaTime;                    float t = startTime * smooting;                    rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, targethorizontal, t);                    // rect2.horizontalNormalizedPosition = Mathf.Lerp(rect2.horizontalNormalizedPosition, targethorizontal, t);                    if (t >= 1)                        stopMove = true;                }            }        }        public void pageTo(int index)        {            if (!isInit) return;            if (index >= 0 && index < posList.Count)            {                rect.horizontalNormalizedPosition = posList[index];                SetPageIndex(index);            }            else            {                Debug.LogWarning("页码不存在");            }        }        private void SetPageIndex(int index)        {            if (!isInit) return;            if (currentPageIndex != index)            {                currentPageIndex = index;                if (OnPageChanged != null)                    OnPageChanged(index);            }        }        public void OnBeginDrag(PointerEventData eventData)        {            if (!isInit) return;            isDrag = true;            startDragHorizontal = rect.horizontalNormalizedPosition;            // startDragHorizontal = rect2.horizontalNormalizedPosition;        }        public void OnEndDrag(PointerEventData eventData)        {            if (!isInit) return;            float posX = rect.horizontalNormalizedPosition;            posX += ((posX - startDragHorizontal) * sensitivity);            posX = posX < 1 ? posX : 1;            posX = posX > 0 ? posX : 0;            int index = 0;            float offset = Mathf.Abs(posList[index] - posX);            for (int i = 1; i < posList.Count; i++)            {                float temp = Mathf.Abs(posList - posX);                if (temp < offset)                {                    index = i;                    offset = temp;                }            }            SetPageIndex(index);            targethorizontal = posList[index]; //设置当前坐标,更新函数进行插值              isDrag = false;            startTime = 0;            stopMove = false;        }        public void SetIndexMove(int index)        {            if (index < 0)            {                index = index < 0 ? posList.Count - 1 : index;            }            else if (index >= posList.Count)            {                index = index >= posList.Count ? 0 : index;            }            SetPageIndex(index);            targethorizontal = posList[index]; //设置当前坐标,更新函数进行插值            startTime = 0;            stopMove = false;        }    }
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-9-23 04:29 , Processed in 0.081059 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表