·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> [iOSUI进阶-3.1]触摸事件的传递

[iOSUI进阶-3.1]触摸事件的传递

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23
A.事件的产生和传递 发生触摸事件后,系统会将该事件加入到一个由UIapplication管理的事件队列中

UIApplication会从事件队列中取出最前面的事件,并将事件分发下去以便处理,通常,先发送事件给应用程序的主窗口(keyWindow)

主窗口会在视图层次结构中找到一个最合适的视图来处理触摸事件,这也是整个事件处理过程的第一步

找到合适的视图控件后,就会调用视图控件的touches方法来作具体的事件处理
touchesBegan…
touchesMoved… touchedEnded…   Image(5)   B.不接受事件传递的情况 不接收用户交互
userInteractionEnabled = NO

隐藏
hidden = YES

透明
alpha = 0.0 ~ 0.01

提示:UIImageView的userInteractionEnabled默认就是NO,因此UIImageView以及它的子控件默认是不能接收触摸事件的   C.触摸事件处理的过程 用户点击屏幕后产生的一个触摸事件,经过一些列的传递过程后,会找到最合适的视图控件来处理这个事件

找到最合适的视图控件后,就会调用控件的touches方法来作具体的事件处理
touchesBegan…
touchesMoved…
touchedEnded…

这些touches方法的默认做法是将事件顺着响应者链条向上传递,将事件交给上一个响应者进行处理   D.响应者链条 Image(6)

For the app on the left, the event follows this path:

  1. The initial view attempts to handle the event or message. If it can’t handle the event, it passes the event to itssuperview, because the initial view is not the top most view in its view controller’s view hierarchy.

  2. The superview attempts to handle the event. If the superview can’t handle the event, it passes the event to its superview, because it is still not the top most view in the view hierarchy.

  3. The topmost view in the view controller’s view hierarchy attempts to handle the event. If the topmost view can’t handle the event, it passes the event to its view controller.

  4. The view controller attempts to handle the event, and if it can’t, passes the event to the window.

  5. If the window object can’t handle the event, it passes the event to the singleton  app object.

  6. If the app object can’t handle the event, it discards the event.

The app on the right follows a slightly different path, but all event delivery paths follow these heuristics:

  1. A view passes an event up its view controller’s view hierarchy until it reaches the topmost view.

  2. The topmost view passes the event to its view controller.

  3. The view controller passes the event to its topmost view’s superview.

    Steps 1-3 repeat until the event reaches the root view controller.

  4. The root view controller passes the event to the window object.

  5. The window passes the event to the app object.

  渣翻译: 对于左边的app来说,事件轨迹是这样的:(单控制器) 1.初始控件尝试处理事件或者信息,如果它不能处理,因为初始控件不是它所在控件控制器的控件层的最顶层控件,所以传递到它的父控件。 2.“父控件”也尝试跟进处理事件,如果这个“父控件”还是不能处理,由于此"父控件"还不是控制器控件层的最顶层控件,所以还会传递这个事件给它的父控件(嗯,初始控件的“爷控件”)。 3.控制器空间层的最顶层控件拿到事件,努力想进行处理,如果它还是没有这个能力,就会传递到它所属的控制器。 4.控制器还不行,只能传递给窗口(window)了 5.window不能处理的话,还会继续传递给app单例对象(Application) 6.如果最后app都不能处理,只能放弃这个事件了。   对于右边的app来讲,事件轨迹稍稍不同,不过也都遵循下列规则:(多控制器) 1.一个控件按照它的控制器控件层次往上传递事件,在此控制器控件层中传递匹配有处理能力的控件,直到到达最顶层控件。 2.上述控件层的最顶层控件不能处理,把事件传递给控制器。 3.如果“2”的控制器不能处理,把事件传递给它的父控件,重复1-3,直到处理了事件,或者到达根控制器。 4.如果根控制器不能处理,传递事件给window对象 5.如果window对象也不能处理,传递事件给app对象(application) 6.app最后也不能处理,舍弃事件     D.监听触摸事件 如果想监听一个view上面的触摸事件,之前的做法是
自定义一个view
实现view的touches方法,在方法内部实现具体处理代码

通过touches方法监听view触摸事件,有很明显的几个缺点
必须得自定义view
由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件
不容易区分用户的具体手势行为

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