《Event Handling Guide for iOS》读书笔记(1)

      昨天面试时,被问到了触摸事件的responder chain,感觉没回答好。很久之前看过那段描述的,时间太久了,记忆有点模糊了,现找到官方资料重新复习一遍。

Event Delivery: The Responder Chain

When a user-generated event occurs, UIKit creates an event object containing the information needed to process the event. Then it places the event object in the active app’ s event queue. For touch events, that object is a set of touches packaged in a UIEvent object.当用户触摸事件发生时,UIKit把有关触摸的信息包装成UIEvent对象,并放置在app的event queue中。

An event travels along a specific path until it is delivered to an object that can handle it. First, the singleton UIApplication object takes an event from the top of the queue and dispatches it for handling. Typically , it sends the event to the app’ s key window object, which passes the event to an initial object for handling.事件会按照一定的机制传递,直到被处理并响应。UIApplication会从event queue中取出这个事件,并dispatch分发出去。一般而言,UIApplication先将这个事件传递给app的key window。

Hit-testing involves checking whether a touch is within the bounds of any relevant view objects. If it is, it recursively checks all of that view’s subviews. The lowest view in the view hierarchy that contains the touch point becomes the hit-test view. After iOS determines the hit-test view, it passes the touch event to that view for handling.通过hit-testing找到最lowest的view,然后IOS就把事件传递给hit-test view处理。

根据app configurations,responder chain有2个种:

1)hit-test view传递给topmost parent,topmost view传递给其controller,controller传递给window

2)hit-test view传递给topmost parent,topmost view传递给其controller,controller传递给比topmost view还要topmost的view,然后再传递给controller,再传递给window

坑爹的是,guide没说怎么配置app configurations,所以,我也不知道到底用哪种方式事件传递。

      经过搜索得知,第2种方式是对第1种的补充,即是当前Controller还有一个父Controller。子Controller处理不了的事件,要丢给父Controller的View,然后丢给父Controlle来处理。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS开发中经常会涉及到触摸事件。本想自己总结一下,但是遇到了这篇文章,感觉总结的已经很到位,特此转载。作者:L...
    WQ_UESTC阅读 6,148评论 4 26
  • 一. Hit-Testing 什么是Hit-Testing?对于触摸事件, window首先会尝试将事件交给事件触...
    面糊阅读 904评论 0 50
  • 最近,由“中国首善”陈光标投资的天杞园减肥特膳在网上热传,无数胖友现身说法,月减20斤,减肥前后身材相貌判若两人。...
    dear_米粒阅读 1,071评论 0 0
  • 前几天老同学打电话邀我一起吃饭,其实也不是那种很久不见的人,一年也能见那么几次,这次我同以前一样带着叙旧的情绪前去...
    梦深深处阅读 1,220评论 0 3
  • 昨天周六,平安夜,和家人出去吃了饭,本来准备看场电影,结果一看,票价翻了三倍,算了。 表妹夫在耐克工厂店,于是我们...
    春春姐阅读 234评论 0 1