zoukankan      html  css  js  c++  java
  • 优化ScrollRect ,实现自动定位到Item中心

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    using Spine;
    using Spine.Unity;
    using System;
    using Global;
    using Kernal;
    using UnityEngine.EventSystems;
    
    namespace Controll
    {
        public class ItemDetailTip_Scroll : MonoBehaviour, IBeginDragHandler, IEndDragHandler,IDragHandler
        {
            [SerializeField]
            private ScrollRect _scrollRect;
    
            [SerializeField]
            private RectTransform _contentTrans;
    
            public float Smoothing = 4f;
    
            private  float[] _pageArry;
    
            private float _targetHorizontalPos = 0;
    
            private bool _isDraging = false;
    
            [SerializeField]
            private float _offset = 0.1f;
    
            private bool _isDragLeft = true;
            private float _curHPosx = 0;
    
            [SerializeField]
            private bool _isTest = false;
    
            [SerializeField]
            private Toggle[] _toggleList;
    
            
           
            public void Init(float width,float[] pageArry,int pos) 
            {
                _curHPosx = _scrollRect.horizontalNormalizedPosition;
                _contentTrans.sizeDelta = new Vector2(width, 1080);
                _pageArry = new float[pageArry.Length];
                _pageArry = pageArry;
                gameObject.SetActive(true);
                _targetHorizontalPos = _pageArry[pos];
                _scrollRect.horizontalNormalizedPosition = _targetHorizontalPos;
               for(int i=0;i<_toggleList.Length;i++)
                {
                    if(i<pageArry.Length)
                    {
                        _toggleList[i].gameObject.SetActive(true);
                    }else
                    {
                        _toggleList[i].gameObject.SetActive(false);
                    }
                }
    
                _toggleList[pos].isOn = true;
            }
            private void Update()
            {
               // Log.Debug(_scrollRect.horizontalNormalizedPosition);
                if (!_isDraging)
                {
                    if (_isTest) return;
                    _scrollRect.horizontalNormalizedPosition = Mathf.Lerp(_scrollRect.horizontalNormalizedPosition,
                   _targetHorizontalPos, Time.deltaTime * Smoothing);
                }
            }
    
            public void OnBeginDrag(PointerEventData eventData)
            {
                _isDraging = true;
            }
            public void OnDrag(PointerEventData eventData)
            {
                if (_scrollRect.horizontalNormalizedPosition>_curHPosx)
                {
                    _isDragLeft = true;
                }else
                {
                    _isDragLeft = false;
                }
                _curHPosx = _scrollRect.horizontalNormalizedPosition;
            }
            public void OnEndDrag(PointerEventData eventData)
            {
                _isDraging = false;
                float posX = _scrollRect.horizontalNormalizedPosition;
                int index = 0;
                float offset = Mathf.Abs(_pageArry[index]-posX);
                for (int i = 0; i < _pageArry.Length; i++)
                {
                    float offsetTmp = Mathf.Abs(_pageArry[i] - posX);
                    if(_isDragLeft)
                    {
                        _offset = Mathf.Abs(_offset);
                    }else
                    {
                        _offset = -Mathf.Abs(_offset);
                    }
                    if(offsetTmp<offset+ _offset)
                    {
                        index = i;
                        offset = offsetTmp;
                    }
                }
                _targetHorizontalPos = _pageArry[index];
                _toggleList[index].isOn = true;
            }
    
           
        }
    }

    效果:

  • 相关阅读:
    如何安装Tomcat服务器
    浅谈数据库中的锁机制
    彻底理解js中this的指向
    Javascript模块化编程的写法
    滚屏加载--无刷新动态加载数据技术的应用
    JavaScript正则表达式
    CSS:水平居中与垂直居中
    Linux常用命令大全
    HTML的元素嵌套规则
    clearfix清除浮动进化史
  • 原文地址:https://www.cnblogs.com/weiqiangwaideshijie/p/10271103.html
Copyright © 2011-2022 走看看