栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

滚动缩放关卡选择列表

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

滚动缩放关卡选择列表

#region 列表滑动方式
        //开始拖拽content
        public void BeginDrag_ScrollRect(PointerEventData eventData)
        {
            beginSmooth = false;
            pointBeginDragX = Input.mousePosition.x;
            Vector2 mouseDown = eventData.position;    //记录鼠标按下时的屏幕坐标
            Vector2 mouseUguiPos = new Vector2();
            bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(m_ContentRectTrans.parent.GetComponent(), mouseDown, eventData.enterEventCamera, out mouseUguiPos);
            if (isRect)   //如果在
            {
                //计算图片中心和鼠标点的差值
                offsetMouse = m_ContentRectTrans.anchoredPosition - mouseUguiPos;
                isTonext = false;
            }
        }
        //拖拽中content
        public void OnDrag_ScrollRect(PointerEventData eventData)
        {

            float x = m_ContentRectTrans.anchoredPosition.x;
            float y = m_ContentRectTrans.anchoredPosition.y;

            int index = curIndex;
            float distance = (m_CellObjectWidth + m_Spacing) / 2f ;
            if (Input.mousePosition.x - pointBeginDragX > distance)
            {
                if (index > 0)
                {
                    pointBeginDragX = Input.mousePosition.x;
                    isTonext = true;
                    JumpCenterIndex(index - 1);
                }
            }
            else if ((Input.mousePosition.x - pointBeginDragX) < distance && (Input.mousePosition.x - pointBeginDragX) > 0)
            {
                SelectContentPos_InDrag(eventData, y);
            }
            else if ((Input.mousePosition.x - pointBeginDragX) > -distance && (Input.mousePosition.x - pointBeginDragX) < 0)
            {
                SelectContentPos_InDrag(eventData, y);
            }
            else if (Input.mousePosition.x - pointBeginDragX < -distance)
            {
                if (index < m_CellInfos.Length-1)
                {
                    pointBeginDragX = Input.mousePosition.x;
                    isTonext = true;
                    JumpCenterIndex(index + 1);
                }
            }
        }
        public void SelectContentPos_InDrag(PointerEventData eventData, float y)
        {
            if (!isToNext)
            {
                Vector2 mouseDrag = eventData.position;   //当鼠标拖动时的屏幕坐标
                Vector2 uguiPos = new Vector2();   //用来接收转换后的拖动坐标
                                                   //和上面类似
                bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(m_ContentRectTrans.parent.GetComponent(), mouseDrag, eventData.enterEventCamera, out uguiPos);

                if (isRect)
                {
                    //设置图片的ugui坐标与鼠标的ugui坐标保持不变
                    m_ContentRectTrans.anchoredPosition = new Vector2((offsetMouse + uguiPos).x, y);
                    if ((offsetMouse + uguiPos).x > rangs[0])
                    {
                        m_ContentRectTrans.anchoredPosition = new Vector2(rangs[0], y);
                    }
                    if ((offsetMouse + uguiPos).x < rangs.Last())
                    {
                        m_ContentRectTrans.anchoredPosition = new Vector2(rangs.Last(), y);
                    }
                }
            }
            else
            {
                Vector2 mouseDown = eventData.position;    //记录鼠标按下时的屏幕坐标
                Vector2 mouseUguiPos = new Vector2();
                bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(m_ContentRectTrans.parent.GetComponent(), mouseDown, eventData.enterEventCamera, out mouseUguiPos);
                if (isRect)   //如果在
                {
                    //计算图片中心和鼠标点的差值
                    offsetMouse = m_ContentRectTrans.anchoredPosition - mouseUguiPos;
                }
            }
        }
        //结束拖拽content
        public void EndDrag_ScrollRect(PointerEventData eventData)
        {
            offsetMouse = Vector2.zero;
            StartSmoothDamp();
        }

        public void SetRangsContent()
        {
            rangs = new List();
            for (int i = 0; i < m_CellInfos.Length; i++)
            {
                float dragContent = (m_PlaneWidth - m_CellObjectWidth) / 2f - (m_CellObjectWidth + m_Spacing) * i;
                rangs.Add(dragContent);
            }
        }

        public void StartSmoothDamp()
        {
            if (smooth != null)
            {
                CoroutingTool.Instance.StopCoroutine(smooth);
            }
            smooth = SmoothDamp();
            beginSmooth = true;
            CoroutingTool.Instance.StartCoroutine(SmoothDamp());
        }
        public IEnumerator SmoothDamp()
        {
            float mMoveSpeed = 0f;
            float SMOOTH_TIME = 0.2f;
            //float SMOOTH_TIME = 0.02f;
            while (beginSmooth)
            {
                float x = m_ContentRectTrans.anchoredPosition.x;
                float y = m_ContentRectTrans.anchoredPosition.y;
                int index = curIndex;
                if (Mathf.Abs((int)x - rangs[index]) < 2f)
                {
                    x = rangs[index];
                    beginSmooth = false;
                    isTonext = false;
                    yield break;
                }
                m_ContentRectTrans.anchoredPosition =
                    new Vector2(Mathf.SmoothDamp(x, rangs[index], ref mMoveSpeed, SMOOTH_TIME), y);

                yield return 0;
            }
        }
        #endregion
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/687011.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号