• UIResponder学习


    http://blog.csdn.net/jimzhai/article/details/23283515

    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对象组成的

    responder chain是由一系列responder对象连接起来的,他从第一个responder对象开始一直到application对象结束。如果第一个responder不能够处理该事件则该事件会被发送到下一个在该responder chain中的responder来处理。
    当自己定义的一个类想让他成为first responder时需要做两件事:
    1.重写 canBecomeFirstResponder 方法让他返回YES
    2.接受 becomeFirstResponder 消息,如果必要的话可让对象给自己发送该消息。
     
    在这里有一个地方需要注意,当把一个对象变为first responder是要确保这个对象的图形界面已经建立起来,也就是说要在viewDidAppear中调用becomeFirstResponder方法。如果在veiwWillAppear方法中调用becomeFirstResponder将会得到一个NO。
     

    Responder Chain 遵循一个特定的传播路径

    如果hit-test view不能够处理该事件则UIKit会将事件传递给下一个Responder。下图则显示了事件在Responder Chain中传播的两种方式:
    对于左边的app中事件传播路径如下:
    1.初始的界面尝试去处理事件后者消息,打他处理不了则把事件交给它上一层视图处理,因为最开始的界面在他的view controller里的视图层次里不是最上层的。(这里的上下是按照树的结构而言的,下图解释:)
    Art/view_hierarchy_relationships.jpg
     
    2.上层视图尝试处理事件,如果他不能处理则将事件交给他的上层视图处理,原因同上。
    3.在view controller中最上层的视图尝试处理,他也不能处理则交给他的view controller来处理。
    4.view controller也无法处理则交给window来处理。
    5.window无法处理交给app object来处理
    6.app object无法处理则将该事件丢弃掉。
    右边的传播方式稍有不同:
    1.一个视图在他的view controller 的视图层中向上传播一个事件直到它到达最顶层视图。
    2.最顶层视图无法处理将event交给他的view controller来处理。
    3.view controller 传递事件到他的最顶层视图的上一层视图,接下来重复1-3的步骤直到事件到达root view controller。
    4.root view controller将事件传递到window object。
    5.window 将事件传递给app object。
     
    注意:事件,消息不要自己向上传送而要调用父类中的方法来处理,让UIKit来处理消息在responder chain中的传递。
     
    Responder chain Demo:
    [objc] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. /*PMBViewController*/  
    2.   
    3. #import "PMBViewController.h"  
    4. #import "PMBViewOne.h"  
    5. #import "PMBVIewTwo.h"  
    6. #import "PMBVIewThree.h"  
    7.   
    8. @interface PMBViewController ()  
    9. @property (weak, nonatomic) IBOutlet PMBVIewThree *viewThree;  
    10.   
    11. @property (weak, nonatomic) IBOutlet PMBVIewTwo *viewTwo;  
    12.   
    13. @property (weak, nonatomic) IBOutlet PMBViewOne *viewOne;  
    14.   
    15.   
    16. @end  
    17.   
    18. @implementation PMBViewController  
    19.   
    20. - (void)viewDidLoad  
    21. {  
    22.     [super viewDidLoad];  
    23.     // Do any additional setup after loading the view, typically from a nib.  
    24. }  
    25.   
    26.   
    27. - (void)didReceiveMemoryWarning  
    28. {  
    29.     [super didReceiveMemoryWarning];  
    30.     // Dispose of any resources that can be recreated.  
    31. }  
    32. @end  
    33.   
    34. /*PMBViewOne*/  
    35. #import "PMBViewOne.h"  
    36.   
    37. @implementation PMBViewOne  
    38. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{  
    39.     [super touchesBegan:touches withEvent:event];  
    40.     [[[touches anyObject] view] setBackgroundColor:[UIColor redColor]];  
    41. }  
    42. @end  
    43.   
    44. /*PMBVIewTwo*/  
    45. #import "PMBVIewTwo.h"  
    46.   
    47. @implementation PMBVIewTwo  
    48. @end  
    49.   
    50. /*PMBVIewThree*/  
    51. #import "PMBVIewThree.h"  
    52.   
    53. @implementation PMBVIewThree  
    54.   
    55. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{  
    56.     [super touchesBegan:touches withEvent:event];  
    57.     [[[touches anyObject] view] setBackgroundColor:[UIColor greenColor]];  
    58. }  
    59.   
    60. @end  
  • 相关阅读:
    单例
    ASIHttpRequest加载网络数据和上传数据功能
    JSONModel
    KVC/KVO
    C中的动态内存分配和释放(free)
    IOS开发中Xcode一些使用技巧,快捷键的使用
    Struts2文件上传
    Struts2的输入校验
    struts2类型转换中的错误处理
    struts2自定义类型转换器
  • 原文地址:https://www.cnblogs.com/zxykit/p/5622484.html
Copyright © 2020-2023  润新知