Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

手势识别与触摸事件,Responder Chain #81

Open
ShannonChenCHN opened this issue Jul 30, 2017 · 1 comment
Open

手势识别与触摸事件,Responder Chain #81

ShannonChenCHN opened this issue Jul 30, 2017 · 1 comment

Comments

@ShannonChenCHN
Copy link
Owner

No description provided.

@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented Jul 30, 2017

手势识别与触摸事件,Responder Chain

相关 Demo 戳👉这里

关键字

  • Responder Chain
  • UIResponder
  • UIEvent
  • UIGestureRecognizer

1、hitTest方法: 当一个事件发生后,事件会从父控件传给子控件,也就是说由UIApplication -> UIWindow -> UIView -> initial view,以上就是事件的传递,也就是寻找最合适的view的过程。

2、touchBegin系列方法: 接下来是事件的响应。首先看initial view能否处理这个事件,如果不能则会将事件传递给其上级视图(inital view的superView);如果上级视图仍然无法处理则会继续往上传递;一直传递到视图控制器view controller,首先判断视图控制器的根视图view是否能处理此事件;如果不能则接着判断该视图控制器能否处理此事件,如果还是不能则继续向上传 递;(对于第二个图视图控制器本身还在另一个视图控制器中,则继续交给父视图控制器的根视图,如果根视图不能处理则交给父视图控制器处理);一直到 window,如果window还是不能处理此事件则继续交给application处理,如果最后application还是不能处理此事件则将其丢弃

3、在事件的响应中,如果某个控件实现了touches...方法,则这个事件将由该控件来接受,如果调用了[supertouches….];就会将事件顺着响应者链条往上传递,传递给上一个响应者;接着就会调用上一个响应者的touches….方法

传递事件的方法(找到响应者):hitTest:withEvent:

只要事件一传递给一个控件,这个控件就会调用他自己的 hitTest:withEvent:方法,这个方法所做的事是寻找并返回最合适的view(能够响应事件的那个最合适的view)。

处理事件的方法(找到处理者):-touchesBegan:withEvent:

如果这个方法没有被实现或者直接调用 super,也就是说这个 view 不能处理触摸事件,系统会将事件传递给响应者链条的下一级。

实际案例

问题:

  • 触摸事件是如何传递的?
  • 如何使得 view 中超出其 bounds 的 subview 能够响应点击事件?

参考资料:

@ShannonChenCHN ShannonChenCHN changed the title 手势识别与触摸事件 手势识别与触摸事件,Responder Chain Jul 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant