响应者链就是当子视图不响应,父视图有响应事件,父视图响应
#import "RootViewController.h" #import "ResponderView.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ResponderView *viewTop = [[ResponderView alloc] initWithFrame:CGRectMake(0, 0, 320, 284)]; viewTop.tag = 100; viewTop.backgroundColor = [UIColor redColor]; [self.view addSubview:viewTop]; [viewTop release]; ResponderView *viewDow = [[ResponderView alloc] initWithFrame:CGRectMake(0, 284, 320, 284)]; viewDow.tag = 101; viewDow.userInteractionEnabled = NO; viewDow.backgroundColor = [UIColor yellowColor]; [self.view addSubview:viewDow]; [viewDow release]; ResponderView *viewNe1 = [[ResponderView alloc] initWithFrame:CGRectMake(40, 40, 240, 204)]; viewNe1.tag = 102; viewNe1.backgroundColor = [UIColor greenColor]; [viewDow addSubview:viewNe1]; [viewNe1 release]; ResponderView *viewNe2 = [[ResponderView alloc] initWithFrame:CGRectMake(40, 40, 160, 124)]; viewNe2.tag = 103; viewNe2.backgroundColor = [UIColor blueColor]; [viewNe1 addSubview:viewNe2]; [viewNe2 release]; UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 100, 100)]; lab.backgroundColor = [UIColor greenColor]; // lab.userInteractionEnabled = NO; lab.tag = 104; [viewTop addSubview:lab]; [lab release]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; // btn.backgroundColor = [UIColor blueColor]; btn.frame = CGRectMake(0, 0, 50,50); btn.tag = 105; //btn.userInteractionEnabled = NO; [btn setTitle:@"按钮" forState:UIControlStateNormal]; [lab addSubview:btn]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"我是视图控制器 我响应!"); } @end
#import "ResponderView.h" @implementation ResponderView - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ switch (self.tag) { case 100: NSLog(@"红色视图"); break; case 101: NSLog(@"黄色视图"); break; case 102: NSLog(@"绿色视图"); break; case 103: NSLog(@"蓝色视图"); break; case 104: NSLog(@"fdsf视图"); break; case 105: NSLog(@"fdsfddddd视图"); break; default: break; } } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ } @end