事件响应链

Posted pengyuan_D

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了事件响应链相关的知识,希望对你有一定的参考价值。

(1)设置根视图控制器

(2)RootViewController.m

- (void)viewDidLoad

    [super viewDidLoad];

    MyView *view = [[MyView alloc] initWithFrame:CGRectMake(0, 20, 320, 300)];
    view.backgroundColor = [UIColor grayColor];
    [self.view addSubview:view];
    


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

    NSLog(@"viewCtrl touchesBegan");
    
    [self.nextResponder touchesBegan:touches withEvent:event];

(3)MyView.m

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

    NSLog(@"myView touchesBegan");
    
    //将事件传递给下一个响应者
    /*
     nextResponder可以取得下一个响应者
     */
    [self.nextResponder touchesBegan:touches withEvent:event];

(4)TouchWindow.m

//window分发事件的方法
- (void)sendEvent:(UIEvent *)event 

    NSLog(@"window sendEvent");
    
    [super sendEvent:event];
    


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

    NSLog(@"window touchesBegan");
    


以上是关于事件响应链的主要内容,如果未能解决你的问题,请参考以下文章

ios中事件的响应链(Responder chain)和传递链

什么是事件响应链

iOS开发 - 事件传递响应链

事件的产生,传递以及响应链

iOS开发 - 事件传递响应链

iOS基础06—--事件响应链