iOS - 点击事件与响应链

一、点击事件涉及到哪些?

  1. touchBegan
  2. uicontrol
  3. gestureRecognizer

二、点击事件如何传递和响应?

  1. 传递链
  2. 响应链
  3. hitTest/pointInside

三、结合以上原理处理的问题和应用

iOS中的事件可以分为3大类型:

  1. 触屏事件(例如点击按钮、通过手势缩放图片、拖动上下滚动页面等)
  2. 传感器事件(例如摇一摇红包、通过旋转设备控制赛车方向、指南针等)
  3. 远程控制事件(例如耳机的线控、外接手柄、遥控器等)

UITouch/UIEvent

点击事件都是基于触屏事件,执行以下代码看看。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print(touches)
        print(event)
    }

我单手指点击了1次屏幕,得到以下结果。

[<UITouch: 0x12edb0120> 
phase: Began 
tap count: 1 
window: <UIWindow: 0x12ed28ad0; 
        frame = (0 0; 375 667); 
        autoresize = W+H; 
        gestureRecognizers = <NSArray: 0x12ed29bc0>;
        layer = <UIWindowLayer: 0x12ed25c90>> 
        
view: <UIView: 0x12ed9b0e0; 
        frame = (0 0; 375 667); 
        autoresize = W+H; 
        layer = <CALayer: 0x12ed9a260>> 
        
location in window: {128, 505} 
previous location in window: {128, 505}

location in view: {128, 505} 
previous location in view: {128, 505}]
Optional(<UITouchesEvent: 0x12ee08a30>
timestamp: 351596 
touches: {(
    <UITouch: 0x12edb0120> 
    phase: Began 
    tap count: 1 
        window: <UIWindow: 0x12ed28ad0; 
        frame = (0 0; 375 667); 
        autoresize = W+H; 
        gestureRecognizers = <NSArray: 0x12ed29bc0>; 
        layer = <UIWindowLayer: 0x12ed25c90>>
        
        view: <UIView: 0x12ed9b0e0; 
        frame = (0 0; 375 667); 
        autoresize = W+H; 
        layer = <CALayer: 0x12ed9a260>>
        
        location in window: {128, 505}
        previous location in window: {128, 505} 
        location in view: {128, 505} 
        previous location in view: {128, 505}
)})

UITouch的Set中的这个UITouch包含了

  1. phase
  2. tap count
  3. window
  4. view
  5. location in window(this/previous)
  6. location in view(this/previous)

这边的UIEvent是自己的子类UITouchesEvent,这边UIEvent包含的属性看起来其实就是UITouch。既然是完全包含关系,为什么要有两个参数呢?

于是我两个手指同时点击屏幕了1次,得到以下结果。

[<UITouch: 0x14c54aea0> phase: Began 
tap count: 1 
window: <UIWindow: 0x14c52bee0; 
        frame = (0 0; 375 667); 
         autoresize = W+H; 
         gestureRecognizers = <NSArray: 0x14c63aa50>;
         layer = <UIWindowLayer: 0x14c639560>> 

view: <UIView: 0x14c52e5b0; 
      frame = (0 0; 375 667); 
      autoresize = W+H; 
      layer = <CALayer: 0x14c52dba0>> 

location in window: {62.5, 444.5} 
previous location in window: {62.5, 444.5}
location in view: {62.5, 444.5} 
previous location in view: {62.5, 444.5}]
Optional(<UITouchesEvent: 0x14c60c8b0> timestamp: 352692 touches: {(
    <UITouch: 0x14c54aea0> phase: Began tap count: 1 window: <UIWindow: 0x14c52bee0; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x14c63aa50>; layer = <UIWindowLayer: 0x14c639560>> view: <UIView: 0x14c52e5b0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x14c52dba0>> location in window: {62.5, 444.5} previous location in window: {62.5, 444.5} location in view: {62.5, 444.5} previous location in view: {62.5, 444.5},
    <UITouch: 0x14c5948d0> phase: Began tap count: 1 window: <UIWindow: 0x14c52bee0; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x14c63aa50>; layer = <UIWindowLayer: 0x14c639560>> view: (null) location in window: {256, 545.5} previous location in window: {256, 545.5} location in view: {256, 545.5} previous location in view: {256, 545.5}
)})

UITouchedEvent我就不展开了,这边看到里面是有两个UITouch,所以1个手指点击1次屏幕是一个UITouch。

那我快速点击屏幕两次。

[<UITouch: 0x14c54b140> phase: Began tap count: 1 window: <UIWindow: 0x14c52bee0; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x14c63aa50>; layer = <UIWindowLayer: 0x14c639560>> view: <UIView: 0x14c52e5b0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x14c52dba0>> location in window: {182, 504} previous location in window: {182, 504} location in view: {182, 504} previous location in view: {182, 504}]
Optional(<UITouchesEvent: 0x14c60c8b0> timestamp: 352928 touches: {(
    <UITouch: 0x14c54b140> phase: Began tap count: 1 window: <UIWindow: 0x14c52bee0; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x14c63aa50>; layer = <UIWindowLayer: 0x14c639560>> view: <UIView: 0x14c52e5b0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x14c52dba0>> location in window: {182, 504} previous location in window: {182, 504} location in view: {182, 504} previous location in view: {182, 504}
)})
[<UITouch: 0x14c54b140> phase: Began tap count: 2 window: <UIWindow: 0x14c52bee0; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x14c63aa50>; layer = <UIWindowLayer: 0x14c639560>> view: <UIView: 0x14c52e5b0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x14c52dba0>> location in window: {182.5, 504.5} previous location in window: {182.5, 504.5} location in view: {182.5, 504.5} previous location in view: {182.5, 504.5}]
Optional(<UITouchesEvent: 0x14c60c8b0> timestamp: 352928 touches: {(
    <UITouch: 0x14c54b140> phase: Began tap count: 2 window: <UIWindow: 0x14c52bee0; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x14c63aa50>; layer = <UIWindowLayer: 0x14c639560>> view: <UIView: 0x14c52e5b0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x14c52dba0>> location in window: {182.5, 504.5} previous location in window: {182.5, 504.5} location in view: {182.5, 504.5} previous location in view: {182.5, 504.5}
)})

看到这边第二次的时候,tap count是2

我慢速点击屏幕2次,结果如下:

[<UITouch: 0x14c54b140> phase: Began tap count: 1 window: <UIWindow: 0x14c52bee0; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x14c63aa50>; layer = <UIWindowLayer: 0x14c639560>> view: <UIView: 0x14c52e5b0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x14c52dba0>> location in window: {182.5, 480} previous location in window: {182.5, 480} location in view: {182.5, 480} previous location in view: {182.5, 480}]
Optional(<UITouchesEvent: 0x14c60c8b0> timestamp: 353057 touches: {(
    <UITouch: 0x14c54b140> phase: Began tap count: 1 window: <UIWindow: 0x14c52bee0; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x14c63aa50>; layer = <UIWindowLayer: 0x14c639560>> view: <UIView: 0x14c52e5b0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x14c52dba0>> location in window: {182.5, 480} previous location in window: {182.5, 480} location in view: {182.5, 480} previous location in view: {182.5, 480}
)})
[<UITouch: 0x14c54b140> phase: Began tap count: 1 window: <UIWindow: 0x14c52bee0; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x14c63aa50>; layer = <UIWindowLayer: 0x14c639560>> view: <UIView: 0x14c52e5b0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x14c52dba0>> location in window: {186, 493.5} previous location in window: {186, 493.5} location in view: {186, 493.5} previous location in view: {186, 493.5}]
Optional(<UITouchesEvent: 0x14c60c8b0> timestamp: 353058 touches: {(
    <UITouch: 0x14c54b140> phase: Began tap count: 1 window: <UIWindow: 0x14c52bee0; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x14c63aa50>; layer = <UIWindowLayer: 0x14c639560>> view: <UIView: 0x14c52e5b0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x14c52dba0>> location in window: {186, 493.5} previous location in window: {186, 493.5} location in view: {186, 493.5} previous location in view: {186, 493.5}
)})

两次Tap count都是1,所以连续点击的时候,时间间隔是一个重要标志。某一个时间段内,会作为count为2次,而过了这个时间段,都变成独立的点击了。所以联想到了tapgesture numberoftaps,应该也是基于这个机制。

这里有个小发现,iOS10及以上在UITouch里面多了Force属性,即压力传感器。当然屏幕压力force属性,在touchBegan和touchEnded是0,只有TouchMove的时候才有值,最大值是6.667,最小是0

[<UITouch: 0x7fb339e07e30> phase: Moved 
tap count: 1 
force: 6.667 
window: <UIWindow: 0x7fb339d05570; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x61800004fe40>; layer = <UIWindowLayer: 0x6180000321c0>> view: <UIView: 0x7fb339d01380; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x618000032700>> location in window: {221.5, 449.5} previous location in window: {221.5, 449.5} location in view: {221.5, 449.5} previous location in view: {221.5, 449.5}]
Optional(<UITouchesEvent: 0x6180000e2580> timestamp: 51618.7 touches: {(
    <UITouch: 0x7fb339e07e30> phase: Moved tap count: 1 force: 6.667 window: <UIWindow: 0x7fb339d05570; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x61800004fe40>; layer = <UIWindowLayer: 0x6180000321c0>> view: <UIView: 0x7fb339d01380; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x618000032700>> location in window: {221.5, 449.5} previous location in window: {221.5, 449.5} location in view: {221.5, 449.5} previous location in view: {221.5, 449.5}
)})

而location的精度是0.5pt。这里关于iOS系统版本对UITouch支持,大家可以看一下UITouch在UIKit中的接口,就是Control+Command+点击进去看看。

Gesture recognizer和UIKit的一些控件如UIButton等都是基于UITouch/UIEvent和Began/Moved/Ended/Cancelled。

iOS处理触屏事件,触屏事件分为两种方式:

高级事件处理:利用UIKit提供的各种用户控件或者手势识别器来处理事件。
低级事件处理:在UIView的子类中重写触屏回调方法,直接处理触屏事件。
如果想监听一个view上面的触摸事件,之前的做法是:

(1)自定义一个view。

(2)实现view的touches方法,在方法内部实现具体处理代码。

通过touches方法监听view触摸事件,有很明显的几个缺点:

(1)必须得自定义view。

(2)由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件。

(3)不容易区分用户的具体手势行为。

UIGestureRecognizer

iOS 3.2后,苹果推出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度。

手势识别器UIGestureRecognizer类的实例也是处理触屏事件的好帮手,其内部也使用目标行为表。
包含了7种手势

  1. UITapGestureRecognizer:点击(单击、双击、三连击等)手势
  2. UIPinchGestureRecognizer:缩放手势
  3. UIPanGestureRecognizer:拖拽手势
  4. UISwipeGestureRecognizer:滑动手势
  5. UIRotationGestureRecognizer:旋转手势
  6. UILongPressGestureRecognizer:长按手势
  7. UIScreenEdgePanGestureRecognizer: 屏幕边缘平移

UIControl

UIKit中我们常用的是UIControl类实例的addTarget:action:forControlEvents:方法维护控件目标行为表,除了UIKit控件外,

点击后如何找到响应者

hitTest

hitTest: withEvent:方法
事件传递的时候调用
当事件传递给控件的时候,就会调用控件的这个方法,去寻找最合适的view
作用:寻找最合适的view

pointInside

pointInside:withEvent:方法
作用:判断当前这个点在不在方法调用者(控件)上
当两个控件有重叠,需要将事件判断是哪一个控件执行的时候

响应是如何传递的

在iOS中只有继承了UIResponder的对象才能接收并处理事件称为响应者对象
UIApplication,UIViewController,UIView都继承自UIResponder,因此他们都是响应者对象, 都能够接收并处理事件。

继承自UIResponder的类能处理事件是由于UIResponder内部提供了以下方法

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {}

通过如下方法传递给下一个响应者

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.next?.touchesBegan(touches, with: event)
}

手势和点击事件重叠如何响应

如何解决手势和手势重叠?

覆盖在上一层的View实现UIGestureRecognizerDelegate协议中的方法

gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer

如何解决手势和单击事件重叠?

UIGestureRecognizer 有个属性cancelsTouchesInView,这个属性默认值是true,即当手势识别成功后,会发送touchesCancelled消息给view来结束view的响应。
如果cancelsTouchesInView为false,那么gestureRecognizer和view都可以响应

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342