iOS 在父视图添加手势,点击子视图,会响应父视图的手势

iOS 在父视图添加手势,点击子视图,会响应父视图的手势,第1张

代码
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                          action:@selector(click)];
    [self.view addGestureRecognizer:tap];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 350, 400);
    button.backgroundColor = [UIColor redColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(kkk) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"我是一个按钮" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:80];
    button.titleLabel.numberOfLines = 0;
    
    
    UILabel *ve = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    ve.backgroundColor = [UIColor cyanColor];
    ve.text = @"我是label1";
    ve.userInteractionEnabled =  YES;
    [self.view addSubview:ve];
    
    UILabel *ve2 = [[UILabel alloc] initWithFrame:CGRectMake(100, 500, 100, 100)];
    ve2.backgroundColor = [UIColor magentaColor];
    ve2.text = @"我是label2";
    ve2.userInteractionEnabled = YES;
    [self.view addSubview:ve2];
    // Do any additional setup aft
    // Do any additional setup after loading the view.
}

- (void)kkk
{
    NSLog(@"按钮按钮");
}

- (void)click
{
    NSLog(@"手势点击手势点击");
}
![请添加图片描述](http://www.kaotop.com/file/tupian/20220518/f828973c6fec4629a1cf224d211fef60.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YiY5YiY5bCP5Y2a,size_20,color_FFFFFF,t_70,g_se,x_16)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/994753.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存