美文网首页
UIButton点击响应

UIButton点击响应

作者: Jorunk | 来源:发表于2018-07-07 09:36 被阅读321次

在一个app中间有一个button,在你手触摸屏幕点击后,到这个button收到点击事件,中间发生了什么呢?

  • 需要先了解一下UIResponder,基本上我们所能看到的所有图形界面都是继承自它的哦,它掌管着操作事件分发大权


    UIResponder
  • 用nextResponder找出上一级响应者

- (IBAction)buttonAction:(id)sender {
    
    UIButton *button = (UIButton *)sender;
    NSLog(@"%@",button);
    NSLog(@"%@",button.nextResponder);
NSLog(@"%@",button.nextResponder.nextResponder);
    NSLog(@"%@",button.nextResponder.nextResponder.nextResponder);
    NSLog(@"%@",button.nextResponder.nextResponder.nextResponder.nextResponder);
    NSLog(@"%@",button.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder);
}
2016-07-08 22:25:56.268 TestButton[55893:12560428] <UIButton: 0x7faad3f1b850; frame = (184 353; 46 30); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x7faad3f1bdc0>>
2016-07-08 22:25:56.269 TestButton[55893:12560428] <UIView: 0x7faad3d27ca0; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x7faad3d0a0d0>>
2016-07-08 22:25:56.269 TestButton[55893:12560428] <ViewController: 0x7faad3c65ed0>
2016-07-08 22:25:56.269 TestButton[55893:12560428] <UIWindow: 0x7faad3e51ab0; frame = (0 0; 414 736); autoresize = W+H; gestureRecognizers = <NSArray: 0x7faad3c67380>; layer = <UIWindowLayer: 0x7faad3c63e30>>
2016-07-08 22:25:56.269 TestButton[55893:12560428] <UIApplication: 0x7faad3c049e0>
2016-07-08 22:25:56.269 TestButton[55893:12560428] <AppDelegate: 0x7faad3d19050>

  • 这个最基本button 的响应链。
  • 注意:此处是通过[self.nextResponder touchesBegan:touches withEvent:event] 传递给下一个 nextResponder 的,一般我们手动重写了 touch 事件时就有可能中断它的过程,当然很多时候是用来监听观察的。


    响应链
  • UIApplication接收到手指的事件之后,就会去调用UIWindow的hitTest:withEvent:,看看当前点击的点是不是在window内,如果是则继续依次调用subView的hitTest:withEvent:方法,直到找到最后需要的view。
/** point :是否在view的frame范围内, event: 传过来的UITouchEvent*/
// 该方法用来判断点击事件发生的位置是否处于当前视图范围内,以确定用户是不是点击了当前视图
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
// 若上述方法返回YES,则向当前视图的所有子视图(subviews)发送下面该事件,直到有子视图返回非空对象或者全部子视图遍历完毕
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
/**
    如所有子视图都返回非,则hitTest:withEvent:方法返回自身(self)。
    也就是找到了最后需要的 View.
*/

hit-test view的官方解释:手指触摸(Touch)操作时会将其打包成一个UIEvent对象,并放入当前活动Application的事件队列,单例的UIApplication会从事件队列中取出触摸事件并传递给单例的UIWindow来处理,UIWindow对象首先会使用hitTest:withEvent:方法寻找此次Touch操作初始点所在的视图(View),即需要将触摸事件传递给其处理的视图,这个过程称之为hit-test view。

  • 注意: hitTest里面是有判断当前的view是否支持点击事件,比如userInteractionEnabledhiddenalpha等属性,都会影响一个view是否可以相应事件,如果不响应则直接返回nil。 所以常常我们一个点击事件不能被除非通常也可能是上述几种原因之一。

  • 同时更详细的iOS事件响应链中Hit-Test View的应用, 推荐看看,作者举例说明了几个常用的扩展,Hit-test view的应用举例还是不错的。

  • 找到了最终的 view啦,看它具体需要做什么啦

 [button addTarget:self action:@selector(buttonTapDoSome) forControlEvents:UIControlEventTouchUpInside];
  • 这个控件对象去触发target对象上的action行为,来最终处理事件。所以此处有顺便了解下Target-Action
  • Target-Action机制由两部分组成:即目标对象和行为Selector。目标对象指定最终处理事件的对象,而行为Selector则是处理事件的方法


    Target-Action机制
  • 最后事件处理完成
整个流程
  • RunLoop这边我大概讲一下

    • 1.主线程的RunLoop被唤醒
    • 2.通知Observer,处理Timer和Source 0
    • 3.Springboard接受touch event之后转给App进程中
    • 4.RunLoop处理Source 1,Source1 就会触发回调,并调用- _UIApplicationHandleEventQueue() 进行应用内部的分发。
    • 5.RunLoop处理完毕进入睡眠,此前会释放旧的autorelease pool并新建一个autorelease pool

相关文章

网友评论

      本文标题:UIButton点击响应

      本文链接:https://www.haomeiwen.com/subject/ljwiuftx.html