zoukankan      html  css  js  c++  java
  • 1.IOS的UI类图和UIResponder(手机事件响应)

    • UI框架的类图

            

    • UI的对象模型

          

    • UIResponder

          

          1、了解了UIResponder的概念后,再来看iOS事件的相关概念,iOS中有三种事件概念:触摸事件、运动事件(motion)、远程控制事件(remote control)。枚举定义如下:

    typedef enum {
        UIEventTypeTouches,
        UIEventTypeMotion,
        UIEventTypeRemoteControl,
    } UIEventType;

    UIResponder的触摸事件的方法:

    -touchesBegan:withEvent:当用户触摸到屏幕时调用方法

    -touchesEnded:withEvent:当用户触摸到屏幕并移动时调用此方法

    -touchesMoved:withEvent:当触摸离开屏幕时调用此方法

    -touchesCancled:withEvent:当触摸被取消时调用此方法

    UIResponder的运动相关事件的方法:(iOS3.0+开始支持)

    -motionBegan:withEvent:运动开始时执行

    -motionEnded:withEvent:运动结束时执行

    -motionCancled:withEvent:运动被取消时执行

    UIResponder的运程控制事件方法:(iOS4.0+开始支持)

    -remoteControlReceivedWithEvent  

    2、接收事件之后,使用到的响应链函数,

    -nextResponder 下一个响应者,在实现中,一般会返回父级对象

    -isFirstResponder  指示对象是否为第一响应者,这里的第一响应者就是当前有焦点的对象,叫法挺奇怪的,第一次看到真还难以理解这个叫法所表达的意思

    -canBecomeFirstResponder  获取一个布尔值,指定对象是否可以获取焦点

    -becomeFirstResponder   把对象设置为 firstResponder 对象

    -canResignFirstResponder   对象是否可以取消 firstResponder 对象

    -resignFirstResponder  取消对象为 firstResponder 对象

    3、在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里的视图层次里不是最上层的。(这里的上下是按照树的结构而言的,下图解释:)
     
    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中的传递。
  • 相关阅读:
    【小错误】ORA-00265: instance recovery required, cannot set ARCHIVELOG mode
    【小错误】Device eth2 has different MAC address than expected, ignoring.
    Bloom filters 布隆过滤器
    ORA-600 [729] "UGA Space Leak" (文档 ID 31056.1)
    Procwatcher: Script to Monitor and Examine Oracle DB and Clusterware Processes (文档 ID 459694.1)
    TECH: Getting a Stack Trace from a CORE file on Unix (文档 ID 1812.1)
    Diagnostic Tools Catalog (文档 ID 559339.1)
    How to Analyze Problems Related to Internal Errors (ORA-600) and Core Dumps (ORA-7445) using My Oracle Support (文档 ID 260459.1)
    windows DOS命令
    收集UNDO管理信息的脚本
  • 原文地址:https://www.cnblogs.com/letsgo/p/4349760.html
Copyright © 2011-2022 走看看