iOS 的事件处理 - hitTest:withEvent: 和 pointInside:withEvent: 是如何相关的? [英] Event handling for iOS - how hitTest:withEvent: and pointInside:withEvent: are related?

查看:11
本文介绍了iOS 的事件处理 - hitTest:withEvent: 和 pointInside:withEvent: 是如何相关的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然大多数苹果文档都写得很好,但我认为 'iOS 事件处理指南' 是一个例外.我很难清楚地理解那里描述的内容.

While most apple documents are very well written, I think 'Event Handling Guide for iOS' is an exception. It's hard for me to clearly understand what's been described there.

文件说,

在命中测试中,一个窗口在视图层次结构的最顶层视图上调用hitTest:withEvent:;此方法通过在视图层次结构中返回 YES 的每个视图上递归调用 pointInside:withEvent: 继续进行,向下进行层次结构,直到找到发生触摸的边界内的子视图.该视图成为命中测试视图.

In hit-testing, a window calls hitTest:withEvent: on the top-most view of the view hierarchy; this method proceeds by recursively calling pointInside:withEvent: on each view in the view hierarchy that returns YES, proceeding down the hierarchy until it finds the subview within whose bounds the touch took place. That view becomes the hit-test view.

那么是不是系统只调用了最顶层视图的hitTest:withEvent:,它调用了所有子视图的pointInside:withEvent:,如果特定子视图的返回值为 YES,则调用该子视图的子类的 pointInside:withEvent:?

So is it like that only hitTest:withEvent: of the top-most view is called by the system, which calls pointInside:withEvent: of all of subviews, and if the return from a specific subview is YES, then calls pointInside:withEvent: of that subview's subclasses?

推荐答案

这似乎是一个很基本的问题.但我同意你的意见,该文件不像其他文件那样清晰,所以这是我的回答.

It seems quite a basic question. But I agree with you the document is not as clear as other documents, so here is my answer.

hitTest:withEvent: 在 UIResponder 中的实现如下:

The implementation of hitTest:withEvent: in UIResponder does the following:

  • 它调用了self
  • pointInside:withEvent:
  • 如果返回 NO,hitTest:withEvent: 返回 nil.故事的结局.
  • 如果返回是 YES,它会发送 hitTest:withEvent: 消息到它的子视图.它从顶级子视图开始,并继续到其他视图,直到一个子视图返回一个非nil 对象,否则所有子视图都会收到消息.
  • 如果子视图第一次返回一个非nil 对象,第一个hitTest:withEvent: 返回那个对象.故事的结局.
  • 如果没有子视图返回非nil对象,第一个hitTest:withEvent:返回self
  • It calls pointInside:withEvent: of self
  • If the return is NO, hitTest:withEvent: returns nil. the end of the story.
  • If the return is YES, it sends hitTest:withEvent: messages to its subviews. it starts from the top-level subview, and continues to other views until a subview returns a non-nil object, or all subviews receive the message.
  • If a subview returns a non-nil object in the first time, the first hitTest:withEvent: returns that object. the end of the story.
  • If no subview returns a non-nil object, the first hitTest:withEvent: returns self

这个过程会递归重复,所以通常最终会返回视图层次结构的叶子视图.

This process repeats recursively, so normally the leaf view of the view hierarchy is returned eventually.

但是,您可以覆盖 hitTest:withEvent 以做不同的事情.在许多情况下,覆盖 pointInside:withEvent: 更简单,并且仍然提供足够的选项来调整应用程序中的事件处理.

However, you might override hitTest:withEvent to do something differently. In many cases, overriding pointInside:withEvent: is simpler and still provides enough options to tweak event handling in your application.

这篇关于iOS 的事件处理 - hitTest:withEvent: 和 pointInside:withEvent: 是如何相关的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆