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

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

  • 相关阅读:
    ASP.NET AJAX入门系列(5):使用UpdatePanel控件(二)
    ASPxGridView之MasterDetail
    ASPxGridView子项动态邦定
    锐浪报表不能访问grf文件
    如何把string解析为int?[C#]
    今天你多态了吗?
    接口继承的声明问题 [C#, BCL]
    如何判断字符串是否为空串?[C#]
    尝鲜微软新桌面主题——Energy Blue
    JSON数据之使用Fastjson进行解析(一)
  • 原文地址:https://www.cnblogs.com/qinshuaijun/p/10930666.html
Copyright © 2011-2022 走看看