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

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

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

具体代码如下:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    MyUIView *myUIView = [[MyUIView alloc]initWithFrame:CGRectMake(0, 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);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesEnded is call");
   
}

@end
#import "MyUIView.h"

@implementation MyUIView

-(instancetype)init{
    self  = [super init];
        return self;
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"111111111");
}

@end

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

运行结果如下


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

【热门文章】
【热门文章】