1:When
a touch occurs inside a specific view, the system sends an event object with the touch information directly to
that view for handling. However, if the view does not handle a particular touch event, it can pass the event
object along to its superview. If the superview does not handle the event, it passes the event object to its
superview, and so on up the responder chain
测试:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
view.backgroundColor = [UIColor blueColor];
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewClick:)];
[view addGestureRecognizer:tapGesture];
UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
view2.center = CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height/2.0);
view2.backgroundColor = [UIColor blackColor];
[self.view addSubview:view];
UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 50)];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
button.center = CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height/2.0);
[view addSubview:button];
[view addSubview:view2];
}
- (void)buttonClick
{
NSLog(@"buttonClick");
}
- (void)viewClick:(UITapGestureRecognizer *)tap
{
int red =arc4random()%255;
int green = arc4random()%255;
int blue = arc4random()%255;
NSLog(@"%d %d %d",red,green,blue);
tap.view.backgroundColor = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
}
测试发现在view2上发生点击事件,由于view2并不响应该事件,于是将事件传递给它的父视图,颜色发生改变,但是该点击事件并不会通过view2传递给button,所以事件传递,仅传递给它的父视图,也就是[superview addsubview:subview]中得superview