UIResponder

作者: yaqiong | 来源:发表于2018-02-05 15:14 被阅读25次
    响应者对象
    Responder objects—that is, 
    instances of UIResponder—constitute the event-handling backbone of a UIKit app. 
    Many key objects are also responders, including the UIApplication object, 
    UIViewController objects, and all UIView objects (which includes UIWindow). 
    As events occur, UIKit dispatches them to your app's responder objects for handling.
    

    响应者对象—即UIResponder的实例—是组成事件处理的核心内容。许多主要的对象都是响应者对象,包括UIApplication、UIViewController以及UIView和继承自UIView的类(包括UIWindow)。当事件触发时,UIKit会将它们分发到app中的响应者对象中进行处理。

    处理事件
    There are several kinds of events, 
    including touch events, motion events, remote-control events, and press events. 
    To handle a specific type of event, a responder must override the corresponding methods.
     For example, to handle touch events, a responder implements the
     touchesBegan(_:with:),
     touchesMoved(_:with:),
     touchesEnded(_:with:), 
     touchesCancelled(_:with:) methods. 
    In the case of touches, 
    the responder uses the event information provided by UIKit to track changes to those
    touches and to update the app's interface appropriately.
    

    事件分为好几种:触摸、运动、遥控、按压(iOS9.0以后可用)等。响应者对象必须实现对应的方法才能处理特定类型的事件。比如,响应者对象可以通过实现以下几个方法来处理触摸事件:
    touchesBegan(:with:)
    touchesMoved(
    :with:)
    touchesEnded(:with:)
    touchesCancelled(
    :with:)
    在触摸事件中,响应者通过UIKite提供的事件信息来追踪这些触摸事件的变化,从而正确地更新界面

    事件转发
    In addition to handling events, UIKit responders also 
    manage the forwarding of unhandled events to other parts of your app. 
    If a given responder does not handle an event, 
    it forwards that event to the next event in the responder chain. 
    UIKit manages the responder chain dynamically, 
    using predefined rules to determine which object should be next to receive an event.
    For example, a view forwards events to its superview, 
    and the root view of a hierarchy forwards events to its view controller.
    

    除了可以处理事件,UIKit的响应者对象也可以将未处理的事件转发到app的其他部分。如果指定的响应者对象没有处理事件,就会将事件转发到响应者链中的下一个响应者那里。UIKite是自动管理响应者链的,通过预定义的一些规则来决定谁是下一个接收事件的响应者。例如:一个view将事件转发给它的superview,层次结构的根view将事件转发给它的视图控制器。

    处理输入视图
    Responders process UIEvent objects but can also accept custom input through an input 
    view. 
    The system's keyboard is the most obvious example of an input view. 
    When the user taps a UITextField and UITextView] object onscreen, the view becomes 
    the first responder and displays its input view, which is the system keyboard. 
    Similarly, you can create custom input views and display them when other responders 
    become active. 
    To associate a custom input view with a responder,assign that view to the inputView
    property of the responder.
    

    响应者对象可以处理事件对象,还可以通过输入视图接受自定义输入。系统键盘是最明显的输入视图的例子。当用户在屏幕上点击UITextField和UITextView时,这个视图就成为了第一响应者同时会展示它的输入视图:系统键盘。同样,你也可以在其他响应者被激活时创建自定义输入视图来显示。为了将自定义视图和响应者联系起来,你需要为响应者对象定义inputView作为它的属性

    总结

    响应者对象定义:UIResponder的实例
    作用:可以处理事件,处理不了的就进行事件转发
    其他作用:处理输入视图,比如键盘输入。常见的UITextField和UITextView,点击时会自动调起系统的输入视图--键盘

    相关文章

      网友评论

        本文标题:UIResponder

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