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

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

  • 相关阅读:
    Heterogeneity Wins
    Android使用ImageView显示网络图片
    Android OOM的解决方式
    洛谷P3390 【模板】矩阵快速幂
    CF732D. Exams[二分答案 贪心]
    洛谷P3388 【模板】割点
    POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
    NOI2001|POJ1182食物链[种类并查集 向量]
    HDU3038 How Many Answers Are Wrong[带权并查集]
    NOIP2010关押罪犯[并查集|二分答案+二分图染色 | 种类并查集]
  • 原文地址:https://www.cnblogs.com/qinshuaijun/p/10930666.html
Copyright © 2011-2022 走看看