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都可以响应

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