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

查看:81
本文介绍了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:,如果从特定子视图返回的是是,然后调用 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.

在UIResponder中执行 hitTest:withEvent:会执行以下操作:

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


  • 它调用 pointInside:withEvent: of self

  • 如果返回为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天全站免登陆