响应链的构成
app中,视图都是按照树状结构组织起来的,构成树状结构的同时也构成了响应链。
找到第一响应者的过程
用户触发某一事件后,UIKIt生成一个UIEvent对象,该对象包含一些处理事件所需信息。处理事件时,UIApplication对象把事件分发给window对象,window对象分发给事件发生的VC视图上,视图按照触摸的位置递归检测其所有子视图,包含触摸点的最底层视图就是hit-testing视图。系统将事件发送给这个视图处理,然后就是视图响应链的过程
响应链的响应过程
1.触发了一个事件,app首先按照树状结构找到第一响应者(通过hit-test方式)
2.如果第一响应者不能处理事件,传递给他的父视图,一直循环到viewController
3.如果viewController无法处理,传递给window对象
4.如果window对象无法处理,传递给UIApplication对象处理,如果还是无法处理,丢弃事件
如果父视图有如上设置
view.hidden=YES;
view.alpha = 0.0f;
view.userInteractionEnabled = NO;
则子视图不能接受触摸事件,包括拦截操作
拦截响应者链
需要拦截响应者对象的View重写
- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event;方法
方法1,返回需要响应的视图(推荐)
方法2,返回自己,但是因为寻找响应者的过程是后添加的视图先检查,所以有可能没遍历到自己就找到了真正响应的view,拦截失败
管理响应者链
Returns the next responder in the responder chain, or nil
if there is no next responder.
The UIResponder
class does not store or set the next responder automatically, so this method returns nil
by default. Subclasses must override this method and return an appropriate next responder. For example, UIView
implements this method and returns the UIViewController object that manages it (if it has one) or its superview (if it doesn’t). UIViewController similarly implements the method and returns its view’s superview. UIWindow
returns the application object. UIApplication
returns nil
Returns
The next object in the responder chain or nil
if this is the last object in the chain.
- (nullable UIResponder*)nextResponder;
//Tells this object when a physical button is first pressed.
UIKit calls this method when a new button is pressed by the user. Use this method to determine which button was pressed and to take any needed actions.
The default implementation of this method forwards the message up the responder chain. When creating your own subclasses, call super
to forward any events that you do not handle yourself.
Parameters
presses
A set of UIPress instances that represent the new presses that occurred. The phase of each press is set to UIPressPhaseBegan.
event
The event to which the presses belong.
//物理设备回调,UIPress对象告诉你哪个按钮被按了
- (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event ;
//接收到远程设备命令时调用,为了允许分发远程控制事件,我们必须调用UIApplication的beginReceivingRemoteControlEvents方法;而如果要关闭远程控制事件的分发,则调用endReceivingRemoteControlEvents方法。
- (void)remoteControlReceivedWithEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(4_0);
//验证命令,启用或禁用指定的命令
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
//
- (nullable id)targetForAction:(SEL)action withSender:(nullable id)sender
输入视图
@property (nullable, nonatomic, readonly, strong) __kindof UIView *inputView NS_AVAILABLE_IOS(3_2);
@property (nullable, nonatomic, readonly, strong) __kindof UIView *inputAccessoryView NS_AVAILABLE_IOS(3_2);
当对象变为第一响应者时,显示另外的视图用来处理信息输入,比如UITextField,UITextView的键盘,自定义键盘就是用自定义的view替换
//视图立即被替换
-(void)reloadInputViews
获取undoManager
可以用来执行对应视图的undo和redo操作
http://www.th7.cn/Program/IOS/201603/774913.shtml
http://southpeak.github.io/2015/03/07/cocoa-uikit-uiresponder/
网友评论