UIResponder

响应链的构成

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/

https://segmentfault.com/a/1190000004529341

http://www.jianshu.com/p/6dbd931eefb0

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • //// UIResponder.h// UIKit//// Copyright (c) 2005-2016...
    李某lkb阅读 800评论 0 0
  • 一. Hit-Testing 什么是Hit-Testing?对于触摸事件, window首先会尝试将事件交给事件触...
    面糊阅读 879评论 0 50
  • 好奇触摸事件是如何从屏幕转移到APP内的?困惑于Cell怎么突然不能点击了?纠结于如何实现这个奇葩响应需求?亦或是...
    Lotheve阅读 57,993评论 51 603
  • 本篇包括2部分:前篇是UIResponder的官方API的记录,后篇是对官方API的理解 UIResponder概...
    你好自己阅读 1,451评论 0 7
  • 1、你以为那些瞬间让你美白的面膜能让你一直美白到老吗? 不!可!能!那些面膜会让你老得更快!长期使用可出现多毛、痤...
    阿昱_阅读 201评论 0 0