【问题标题】:Make sibling view ignore any touch input, the other sibling view should receive it使兄弟视图忽略任何触摸输入,其他兄弟视图应该接收它
【发布时间】:2020-01-07 17:36:52
【问题描述】:

我开发了一个日历库,类似于默认的 iOS 日历应用程序日视图(日程)。

日历显示作为UIView 子类的事件。这些事件可以被点击、长按或处理其他基于手势的用户交互。

有时CurrentTimeIndicator 也是一个组合的UIView 子类可能与Event 重叠。

EventCurrentTimeIndicator 共享一个共同的超级视图 TimelineView

它们看起来像这样:

Event 顶部没有CurrentTimeIndicator 时,一切正常,我可以通过点击视图触发用户交互事件。但是,当CurrentTimeIndicatorEvent 重叠时,指示器接收到的触摸事件将被忽略并且不会传递到底层Event 视图。

目标是从响应者链中完全排除CurrentTimeIndicator,但让Event 处理任何触摸事件,即使被CurrentTimeIndicator 遮挡。

查看层次结构:

指示器和事件都是同级视图。当点击突出显示的触摸区域中的事件时,触摸不会被传递。

到目前为止,我已经尝试过使用userInteractionEnabled = true / false,覆盖hitTest:withEvent: 方法。

解决问题的其他方法是什么?

代码参考:

Event View - creating gesture recognizers Timeline - layout current time indicator Current Time Indicator - code

从概念上讲,我正在寻找类似于CALayerzPosition 属性的东西,但用于触摸输入。

【问题讨论】:

    标签: ios swift uikit uigesturerecognizer uievent


    【解决方案1】:

    在 CurrentTimeIndicator 视图中覆盖 pointInside 方法:

      override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
          print("Passing all touches to the next view (if any), in the view stack.")
          return false
      }
    

    在这里回答:iOS - forward all touches through a view

    【讨论】:

    • 感谢您的回答,卡罗琳!令人惊讶的是,我尝试在CurrentTimelineIndicator 中设置isUserInteractionEnabled=false,一切都像魔术一样工作。在此之前,我已将检测逻辑完全移至Timeline,并通过调用hitTest 来检查其上方是否有EventView(类似于在UICollectionView 中使用UITapGestureRecognizer 的方法)。现在继续使用isUserInteractionEnabled=false 方法,但仍然感谢您!
    • 很高兴你把它整理出来:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多