美文网首页iOS 学习
Understanding Event Handling, Re

Understanding Event Handling, Re

作者: 毛毛可 | 来源:发表于2018-06-22 23:25 被阅读0次

    之前将文章发布在了@weakTong那里,自己经过修改,重新发布.
    原文地址

    学习事件是怎样在你的app中传递,并学会应该如何处理它们.

    Apps使用responder(响应者)对象来接受并处理事件.responder对象可以是任意一个UIResponder类的实例,最常见的子类为UIView,UIViewController和UIApplication.

    responder对象接收到原始的事件数据时,必须处理它或者将它继续向前传递给下一个responder对象.当你的app在接收到一个事件时,UIKit框架会自动的找到最合适的responder去处理该事件,这就是所谓的第一响应者.

    未处理的事件会在响应者链中从这个responder传递给下一个responder.事实上你的app中没有单独的响应者链.UIkit定义了responder对象传递的默认规则,当然你可以通过覆盖responder对象中对应的属性来更改规则.

    图一 显示了APP中一条默认的响应者链,包括一个label,一个text field,一个button和两个background view.如果text field不去处理事件,那么UIKit会将事件发送给textfield的父视图对象.接着就是window的root view.从root view出来,会顺着响应者链在到达window对象之前先传递给view controller.如果此时window也不处理此事件,UIKit会将事件分发给UIApplication对象,或者是分发给app delegate,app delegate是一个UIResponder实例,但此过程已经不是响应者链中的一部分了.

    15125310630299.jpg

    Determining the First Responder for an Event

    针对每种类型的事件,UIKit指定了第一响应者,将事件发送给第一响应者.第一响应者的确定是基于事件的类型的.

    Touch事件

    第一响应者就是触摸发生的view.

    Press事件

    第一响应者是获得焦点的响应者对象.

    摇动事件

    第一响应者是你自己指定的相应对象.

    远程控制事件

    第一响应者也是你自己指定的相应对象.

    编辑菜单信息

    第一响应者是你指定的相应对象.

    提示:
    运动事件相关的加速,陀螺仪和磁力计都不会出现在响应者链中.Core MOtion会知己派发这些事件给你对应注册的对象.想了解更多信息,请查看Core Motion Framework

    UIcontrol控件会直接和与它们相关联的target object使用action message进行通信.当用户使用UIControl控件进行交互时,UIControl控件会调用对应的target objectaction message,换句话说UIControl控件会发送一条action message给它的target object.action message并不属于事件.但是它仍然会使用响应者链进行传递.当UIControl的target object为nil时,UIKit框架就会从target object开始,并沿着响应者链进行查找,直到找到一个实现了相关action方法的对象.比如:UIKit编辑菜单就会使用这种行为来寻找responder对象,类似cut:,copy:或者paste:的方法名.

    If a view has an attached gesture recognizer, the gesture recognizer receives touch and press events before the view receives them. If all of the view’s gesture recognizers fail to recognize their gestures, the events are passed to the view for handling. If the view does not handle them, UIKit passes the events up the responder chain. For more information about using gesture recognizer’s to handle events, see Handling UIKit Gestures.

    如果在view中有添加手势识别器,手势识别器会在view之前接收到touch和press事件.如果view中所有的手势识别都没有识别成功,事件会传给view去处理.如果此view没有处理它们,UIKit会继续传给响应者链的上一层.关于更多的手势识别器处理事件,请参考Handling UIKit Gestures.

    Determining Which Responder Contained a Touch Event

    UIKit使用 基于view的hit-testing方式去确定touch事件发生的位置.具体来说,UIKit会将touch的位置与在view层级中的view对象的容器范围比较.UIView的hitTest:withEvent:方法随着view层级,查找包含touch的最深层的子view.这个view将成为touch事件的第一响应者.

    Note

    If a touch location is outside of a view’s bounds, the hitTest:withEvent: method ignores that view and all of its subviews. As a result, when a view’s clipsToBounds property is NO, subviews outside of that view’s bounds are not returned even if they happen to contain the touch. For more information about the hit-testing behavior, see the discussion of the hitTest:withEvent: method in UIView.

    提示:
    如果touch的位置超出了view的bounds,hitTest:withEvent:方法会忽略掉此View及其所有subview.结果就是当View的clipsToBounds属性为NO时,即使包含了touch,View bounds范围外的所有subview也都不会被返回.更多关于hitTest:withEvent:方法的行为,请查看UIViewhitTest:withEvent:方法.

    UIkit总会赋值给view其范围的每个touch.当touch第一次发生时,UIKit创建UITouch对象,直到触摸结束后才释放touch对象.如果触摸的位置或者其他参数改变了,UIKit会及时更新UITouch对象的信息.只有一个属性不可能发生改变就是是否在view范围中.及时当触摸位置移出了原view,touch对象的属性也不会改变.

    Altering the Responder Chain

    你可以通过重载responder对象的nextResponder属性来改变响应者链.你可以在此方法(getter)返回下一个响应者.

    许多UIKit的类已经重载了这个属性,并返回了指定的对象.包括:

    UIView

    UIView对象,如果view是view controll的根视图,那么view的下个响应者就是view controller,否者下一个响应者就是view的父视图.

    UIViewController.

    如果view controller.view是window的根视图,那view controller的下一个响应者就是window对象.

    UIWindow

    window对象.window的下一个responder是UIApplication对象.

    UIApplication

    UIApplication object. The next responder is the app delegate, but only if the app delegate is an instance of UIResponder and is not a view, view controller, or the app object itself.
    UIApplication对象.下一个响应者是app delegate,只有app delegate是非视图类的UIResponder对象.

    相关文章

      网友评论

        本文标题:Understanding Event Handling, Re

        本文链接:https://www.haomeiwen.com/subject/ffglyftx.html