Class
UIResponder
An abstract interface for responding to and handling events.
--提供响应和处理事件的抽象接口
Declaration
class UIResponder : NSObject
Overview --概览
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
实例,UIResponder
是事件处理的骨干。UIApplication
、UIViewController
、UIView
、UIWindow
都是响应者对象,都继承了UIResponder。
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:)
, and 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.
--这里有者几种类型的事件,包括触摸事件、运动事件、遥控事件和按压事件。若要处理特定类型的事件,响应者必须要重写相应的方法。例如,要处理触摸事件,响应者要实现touchesStarted(:with:)、touchesMoved(:with:)、toucheSend(:with:)和touchesCancelled(:with:)方法。在触摸的情况下,响应者使用UIKit提供的事件信息来跟踪这些触摸的更改,并适当地更新app的界面。
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的其他部分。如果给定的响应者不处理事件,那么它就会将该事件转发到响应者链中的下一个响应者。UIKit动态管理响应者链,使用预定义的规则来确定下一个接收事件的对象。例如,视图将事件转发到其父视图,层次结构的根视图将事件转发到其vc。
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.
--响应者能处理UIEvent对象,但也可以通过输入view来接受自定义输入。系统键盘是输入视图最明显的例子。当用户在屏幕上点击UITextField和UITextView对象时,该视图将成为第一个响应程序并显示其输入视图,即系统键盘。类似地,您可以创建自定义输入视图,并在其他响应者激活时显示它们。若要将自定义输入视图与响应者关联,请将该视图分配给响应者的inputView属性。
For information about responders and the responder chain, see Event Handling Guide for UIKit Apps.
--有关响应程序和响应程序链的信息,请参阅UIKit应用程序的事件处理指南。
Topics -- 专题
Managing the Responder Chain --管理响应者链
var next: UIResponder?
--指定下一位响应者,nil则表示没有下一位响应者
Returns the next responder in the responder chain, or nil
if there is no next responder.
Returns a Boolean value indicating whether this object is the first responder.
var canBecomeFirstResponder: Bool
Returns a Boolean value indicating whether this object can become the first responder.
func becomeFirstResponder() -> Bool
Asks UIKit to make this object the first responder in its window.
var canResignFirstResponder: Bool
Returns a Boolean value indicating whether the receiver is willing to relinquish first-responder status.
func resignFirstResponder() -> Bool
Notifies this object that it has been asked to relinquish its status as first responder in its window.
Responding to Touch Events --响应触摸事件
func touchesBegan(Set<UITouch>, with: UIEvent?)
Tells this object that one or more new touches occurred in a view or window.
func touchesMoved(Set<UITouch>, with: UIEvent?)
Tells the responder when one or more touches associated with an event changed.
func touchesEnded(Set<UITouch>, with: UIEvent?)
Tells the responder when one or more fingers are raised from a view or window.
func touchesCancelled(Set<UITouch>, with: UIEvent?)
Tells the responder when a system event (such as a system alert) cancels a touch sequence.
func touchesEstimatedPropertiesUpdated(Set<UITouch>)
Tells the responder that updated values were received for previously estimated properties or that an update is no longer expected.
Responding to Motion Events --响应动作事件
func motionBegan(UIEvent.EventSubtype, with: UIEvent?)
Tells the receiver that a motion event has begun.
func motionEnded(UIEvent.EventSubtype, with: UIEvent?)
Tells the receiver that a motion event has ended.
func motionCancelled(UIEvent.EventSubtype, with: UIEvent?)
Tells the receiver that a motion event has been cancelled.
Responding to Press Events --响应按压事件
Generally, responders that handle press events should override all four of these methods.
func pressesBegan(Set<UIPress>, with: UIPressesEvent?)
Tells this object when a physical button is first pressed.
func pressesChanged(Set<UIPress>, with: UIPressesEvent?)
Tells this object when a value associated with a press has changed.
func pressesEnded(Set<UIPress>, with: UIPressesEvent?)
Tells the object when a button is released.
func pressesCancelled(Set<UIPress>, with: UIPressesEvent?)
Tells this object when a system event (such as a low-memory warning) cancels a press event.
Responding to Remote-Control Events --响应远程控制事件
func remoteControlReceived(with: UIEvent?)
Tells the object when a remote-control event is received.
Managing Input Views --管理输入视图
The custom input view to display when the receiver becomes the first responder.
var inputViewController: UIInputViewController?
The custom input view controller to use when the receiver becomes the first responder.
var inputAccessoryView: UIView?
The custom input accessory view to display when the receiver becomes the first responder.
var inputAccessoryViewController: UIInputViewController?
The custom input accessory view controller to display when the receiver becomes the first responder.
Updates the custom input and accessory views when the object is the first responder.
Getting the Undo Manager --获取撤销管理者
Returns the nearest shared undo manager in the responder chain.
Building and Validating Commands --创建和验证命令
Asks the receiving responder to validate the command.
func canPerformAction(Selector, withSender: Any?) -> Bool
Requests the receiving responder to enable or disable the specified command in the user interface.
func target(forAction: Selector, withSender: Any?) -> Any?
Returns the target object that responds to an action.
Accessing the Available Key Commands --访问可用的关键命令
var keyCommands: [UIKeyCommand]?
The key commands that trigger actions on this responder.
Managing the Text Input Mode --管理文本输入模式
var textInputMode: UITextInputMode?
The text input mode for this responder object.
var textInputContextIdentifier: String?
An identifier signifying that the responder should preserve its text input mode information.
class func clearTextInputContextIdentifier(String)
Clears text input mode information from the app’s user defaults.
var inputAssistantItem: UITextInputAssistantItem
The input assistant to use when configuring the keyboard’s shortcuts bar.
Supporting User Activities --用户活动的支持
var userActivity: NSUserActivity?
An object encapsulating a user activity supported by this responder.
func restoreUserActivityState(NSUserActivity)
Restores the state needed to continue the given user activity.
func updateUserActivityState(NSUserActivity)
Updates the state of the given user activity.
Instance Properties --实例属性
var activityItemsConfiguration: UIActivityItemsConfigurationReading?
var editingInteractionConfiguration: UIEditingInteractionConfiguration
Type Properties --类属性
class let keyboardAnimationCurveUserInfoKey: String
The key for an NSNumber
object containing a UIView.AnimationCurve
constant that defines how the keyboard will be animated onto or off the screen.
class let keyboardAnimationDurationUserInfoKey: String
The key for an NSNumber
object containing a double
that identifies the duration of the animation in seconds.
class let keyboardDidChangeFrameNotification: NSNotification.Name
Posted immediately after a change in the keyboard’s frame.
class let keyboardDidHideNotification: NSNotification.Name
Posted immediately after the dismissal of the keyboard.
class let keyboardDidShowNotification: NSNotification.Name
Posted immediately after the display of the keyboard.
class let keyboardFrameBeginUserInfoKey: String
The key for an NSValue
object containing a CGRect
that identifies the starting frame rectangle of the keyboard in screen coordinates. The frame rectangle reflects the current orientation of the device.
class let keyboardFrameEndUserInfoKey: String
The key for an NSValue
object containing a CGRect
that identifies the ending frame rectangle of the keyboard in screen coordinates. The frame rectangle reflects the current orientation of the device.
class let keyboardIsLocalUserInfoKey: String
The key for an NSNumber
object containing a Boolean that identifies whether the keyboard belongs to the current app. With multitasking on iPad, all visible apps are notified when the keyboard appears and disappears. The value of this key is true
for the app that caused the keyboard to appear and false
for any other apps.
class let keyboardWillChangeFrameNotification: NSNotification.Name
Posted immediately prior to a change in the keyboard’s frame.
class let keyboardWillHideNotification: NSNotification.Name
Posted immediately prior to the dismissal of the keyboard.
class let keyboardWillShowNotification: NSNotification.Name
Posted immediately prior to the display of the keyboard.
Instance Methods
func buildMenu(with: UIMenuBuilder)
func makeTouchBar() -> NSTouchBar?
Relationships
Inherits From
Conforms To
See Also
First Steps
Using Responders and the Responder Chain to Handle Events
Learn how to handle events that propagate through your app.
An object that describes a single user interaction with your app.