zoukankan      html  css  js  c++  java
  • 嵌套列表拖拽事件冲突问题

    今天项目中遇到这类需求,我的项目是循环列表与列表的冲突,导致事件被拦截,苦思两天终于搞定,上代码

    using UnityEngine.UI;
    using UnityEngine.EventSystems;
    using UnityEngine;
    
    /// <summary>
    /// 解决嵌套使用ScrollRect时的Drag冲突问题。请将该脚本放置到内层ScrollRect上(外层的ScrollRect的Drag事件会被内层的拦截)
    /// </summary>
    public class UINestedScrollRect : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
    {
        /// <summary>
        /// 外层被拦截需要正常拖动的ScrollRect,可不指定,默认在父对象中找
        /// </summary>
        public ScrollRect m_AnotherScrollRect;
    
        /// <summary>
        /// 当前的ScrollRect(本脚本所放置的物体上)的拖动方向默认为上下拖动,否则为左右拖动型
        /// </summary>
        public bool m_ThisIsUpAndDown = true;
        private LoopScrollRect m_ThisScrollRect;
    
        void Awake()
        {
            m_ThisScrollRect = GetComponent<LoopScrollRect>();
            if (m_AnotherScrollRect == null)
                m_AnotherScrollRect = GetComponentsInParent<ScrollRect>()[0];
        }
    
        public void OnBeginDrag(PointerEventData eventData)
        {
            m_AnotherScrollRect.OnBeginDrag(eventData);
        }
    
        public void OnDrag(PointerEventData eventData)
        {
            m_AnotherScrollRect.OnDrag(eventData);
            float angle = Vector2.Angle(eventData.delta, Vector2.left);
    
            //判断拖动方向,防止水平与垂直方向同时响应导致的拖动时整个界面都会动
            if (angle > 45f && angle < 135f)
            {
                m_ThisScrollRect.enabled = !m_ThisIsUpAndDown;
                m_AnotherScrollRect.enabled = m_ThisIsUpAndDown;
            }
            else
            {
                m_AnotherScrollRect.enabled = !m_ThisIsUpAndDown;
                m_ThisScrollRect.enabled = m_ThisIsUpAndDown;
            }
        }
        public void OnEndDrag(PointerEventData eventData)
        {
            m_AnotherScrollRect.OnEndDrag(eventData);
            m_AnotherScrollRect.enabled = true;
            m_ThisScrollRect.enabled = true;
        }
    }

    完美解决事件拦截问题,可以根据自己的需求改对应的参数

  • 相关阅读:
    【vue】饿了么项目-goods商品列表页开发
    【vue】饿了么项目-header组件开发
    转转hybrid app web静态资源离线系统实践
    从列表到详情,没你想的那么简单
    浅谈Async/Await
    小程序代码包压缩 策略&方案
    微信小程序使用场景延伸:扫码登录、扫码支付
    这一次带你彻底了解Cookie
    常见函数错误引发的思考.
    触碰密码世界的大门
  • 原文地址:https://www.cnblogs.com/qinshuaijun/p/10930666.html
Copyright © 2011-2022 走看看