【问题标题】:How to pass touch event to other views only when touchtype is finger?仅当触摸类型为手指时,如何将触摸事件传递给其他视图?
【发布时间】:2019-12-08 07:35:57
【问题描述】:

我的视图上有一个子视图,我希望这个子视图只识别铅笔触摸类型。每次触摸类型是手指时,我都希望它被父视图识别。目前我正在使用如下所示的功能。这里的问题是 UIEvent 没有触摸,因此无法检查它是手指还是铅笔触摸。

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    return false
}

【问题讨论】:

  • 为什么不覆盖touchesBegan|Moved|Ended
  • 因为在touchesbegan函数之前调用了“point(inside point: CGPoint, with event: UIEvent?) -> Bool”

标签: ios swift touches


【解决方案1】:

更新

一个选项是在底部视图中禁用用户交互,并在顶部视图中实现:

func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent)
func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent)
func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent)

在这些函数中,你可以用touches.first?.type检查类型,你可以用touches.first?.view检查视图,如果它们满足你的条件,你可以在底部视图上调用touchesBegan/Moved/Ended

之前说的:

使用这三种方法:

func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent)
func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent)
func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent)

你可以用 touches.first?.type 来检查他们的类型,你可以用 touches.first?.view 来检查视图。

您可以通过在下面的视图上调用 touchesBegan/Moved/Ended 来转发触摸。

【讨论】:

  • 这是一个很好的解决方案,但我的观点是 UIScrollview。所以当我通过 tocuhesMoved 什么都没有发生...
  • 这是一个复杂的问题(UIScrollView with touchesMoved)。也许this 能帮上忙?
  • 有没有一种方法可以让我的所有视图都检测到触摸,而不仅仅是一个?
  • ScrollViews 吞噬触摸有点令人讨厌。我认为我在上一条评论中链接到的解决方案会有所帮助。另一种方法(我会这样做)是在两个视图之上有一个简单的视图,用于管理所有触摸并将它们转发到适当的视图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多