码迷,mamicode.com
首页 > 其他好文 > 详细

优化ScrollRect ,实现自动定位到Item中心

时间:2019-01-15 14:12:29      阅读:583      评论:0      收藏:0      [点我收藏+]

标签:erp   图片   lse   content   else   style   event   atime   ons   

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;
        }

       
    }
}

技术分享图片

效果:

技术分享图片

优化ScrollRect ,实现自动定位到Item中心

标签:erp   图片   lse   content   else   style   event   atime   ons   

原文地址:https://www.cnblogs.com/weiqiangwaideshijie/p/10271103.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!