UIResponder
介绍
UIResponder 这个类定义了很多用来处理响应和时间处理的类。他的子类有UIApplication,UIView以及UIWindow等。
IOS中分为两类事件:触摸事件,和移动事件。最原始的事件处理方是touchesBegan:withEvent:
,touchesMoved:withEvent:
, touchesEnded:withEvent:
, and touchesCancelled:withEvent:。
无论任何时候手指只要触摸屏幕或是在屏幕上移动拖拽甚至离开屏幕都会导致一个UIEvent对象产生。
Responder Chain
在UIResponder中有一个非常重要的概念叫做Responder Chain,个人的理解是这是按照一定规则组织的响应、处理事件的一条链表。在了解UIResponder之前还得在了解一个概念Hit-Testing。在IOS中通常使用hit-testing去找到那个被触摸的视图。这个视图叫hit-test view,当IOS找到hit-test view后就把touch event交个那个视图来处理。下面画个图来说明一下,当点击视图E时看一下hit-testing的工作过程。
1.确定改触摸事件发生在view A范围内,接下来测试view B以及view C。
2.检查发现事件不再view B范围内发生,接下来检查view C发现触摸事件发生在了view C中,所以检查 view D,view E。
3.最后发现事件发生在view E的范围内所以view E成为了hit-test view。
下面是关于调用hit-test的官方说明:
The hitTest:withEvent:
method returns the hit test view for a given CGPoint
and UIEvent
. The hitTest:withEvent:
method begins by calling thepointInside:withEvent:
method on itself. If the point passed into hitTest:withEvent:
is inside the bounds of the view, pointInside:withEvent:
returns YES
. Then, the method recursively calls hitTest:withEvent:
on every subview that returns YES
.
Responder Chain 是由responder对象组成的
canBecomeFirstResponder
方法让他返回YES
Responder Chain 遵循一个特定的传播路径
![](https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Art/iOS_responder_chain_2x.png)
![Art/view_hierarchy_relationships.jpg](https://i-blog.csdnimg.cn/blog_migrate/6f36462f6ac81872236a31006f4b259c.jpeg)
- 1.- (UIResponder *)nextResponder
UIResponder类不自动存储和设置下一个响应者,而是默认返回nil。子类必须override这个方法来设置下一个响应者。
UIView实现了这个方法,因为可以返回管理这个UIView的UIViewController或者它的父类;
UIViewController实现了这个方法,返回UIViewController的View的父View;
UIWindow发挥UIApplication对象;
UIApplication返回nil
- 2.- (BOOL)isFirstResponder
- 3.- (BOOL)canBecomeFirstResponder
如果一个响应对象通过这个方法返回YES,那么它成为了第一响应对象,并且可以接收触摸事件和动作消息。
子类必须overrider这个方法才可以成为第一响应者。
You must not send this message to a view that is not currently attached to the view hierarchy. The result is undefined.
- 3.- (BOOL)becomeFirstResponder
子类可以override这个方法来更新状态或者执行一些行为,比如高亮选中项。
一个响应对象只有当前响应者可以放弃第一响应者状态,并且新的响应者可以成为第一响应者,才能成为第一响应对象。
- 4.- (BOOL)canResignFirstResponder
- 5.- (BOOL)resignFirstResponder
如果返回NO,拒绝放弃第一响应状态。
如果你override这个方法,必须调用父类的实现[super resignFirstResponder].
Managing Input Views
- 1.@property (readonly, retain) UIView *inputView
This property is typically used to replace the system-supplied keyboard that is presented for UITextField and UITextView objects.
UITextField和UITextView如果设置了inputView那么在becomeFirstResponder时不会显示键盘,而现实自定义的inputView;如果设置了inputAccessoryView那么在becomeFirstResponder时会在键盘的顶端显示自定义的inputAccessoryView。
- 2.@property (readonly, retain) UIView *inputAccessoryView
This property is typically used to attach an accessory view to the system-supplied keyboard that is presented for UITextField and UITextViewobjects.
- 3.- (void)reloadInputViews
Responding to Touch Events
- 1.- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
将消息转发给下一个响应者,将消息发送给父类,不要将消息直接传递给下一个响应者。
如果你override这个方法而没有调用super..,你必须同样override其它响应触摸事件的方法,你要是空实现就好。
默认是不支持多点触摸的,如果想要响应多点触摸,你只要吧UIView的 multipleTouchEnabled 属性设置为YES即可。
- 2.- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- 3.- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- 4.- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
Responding to Motion Events
- 1.- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
- 2.- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
- 3.- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
Responding to Remote-Control Events
- 1.- (void)remoteControlReceivedWithEvent:(UIEvent *)event
Getting the Undo Manager
- 1.@property(readonly) NSUndoManager *undoManager
Validating Commands
- 1.- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
YES if the the command identified by action should be enabled or NO if it should be disabled. Returning YES means that your class can handle the command in the current context.