昨天面试时,被问到了触摸事件的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来处理。