为什么一个touch事件被响应了两次?

我在ViewController中实现了touchesEnded方法,然后又在ViewController中添加了一个subView,该subView是我自定义的UIView的子类,该类里面也实现了touchedEnded方法。

具体代码如下:

#import "ViewController.h" @interface ViewController @end @implementation ViewController - voidviewDidLoad { MyUIView *myUIView = [[MyUIView alloc]initWithFrame:CGRectMake0, 0, 100, 200 ]; myUIView.backgroundColor = [UIColor blackColor]; [self.view addSubview: myUIView]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSLog@"%@",self.view;
} - voiddidReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.
} -voidtouchesEnded:NSSet<UITouch *> *touches withEvent:UIEvent *event{ NSLog@"touchesEnded is call"; } @end
#import "MyUIView.h" @implementation MyUIView -instancetypeinit{ self = [super init]; return self;
} -voidtouchesEnded:NSSet<UITouch *> *touches withEvent:UIEvent *event{ NSLog@"111111111";
} @end

当我点击下图的黑色部分时,两个touchedEnded方法都发生了响应,这是为什么呢???

运行结果如下

这叫事件路由!
事件是一层一层传递的,像冒泡一样~

发表评论

电子邮件地址不会被公开。 必填项已用*标注