为什么一个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方法都发生了响应,这是为什么呢???
图片描述

图片描述

运行结果如下

阅读 4.4k
1 个回答

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题