ios - 为什么一个touch事件被响应了两次?
高洛峰
高洛峰 2017-04-17 16:51:40
[iOS讨论组]
我在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方法都发生了响应,这是为什么呢???

运行结果如下

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(1)
巴扎黑

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号