父view中添加手势子view不响应的问题解决

本文介绍如何在iOS应用中创建基本的用户界面元素,包括UIImageView、UILabel和UIButton,并实现手势识别功能,确保特定视图不响应手势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//父类view
   
UIImageView *back = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,kScreenWidth,kScreenHeight)];
    back.
image= [UIImageimageNamed:@"Default-568h@2x"];
    back.
userInteractionEnabled= YES;
    [
self.viewaddSubview:back];
   
   
// 父类view中的imageView
   
imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(100,100,100,100)];
   
imageView.image= [UIImageimageNamed:@"Default-568h@2x"];
   
imageView.userInteractionEnabled= YES;
   
   
// 父类view中的label
   
label = [[UILabelalloc]initWithFrame:CGRectMake(100,240,100,100)];
   
label.text= @"label";
   
label.backgroundColor= [UIColorgrayColor];
   
// 此属性必须打开,不然UITouch捕获Label
   
label.userInteractionEnabled= YES;
   
   
// 父类view中的button
   
button = [UIButtonbuttonWithType:UIButtonTypeCustom];
    [
buttonsetTitle:@"button"forState:UIControlStateNormal];
   
button.frame= CGRectMake(100,350,100,100);
   
button.backgroundColor= [UIColorpurpleColor];
    [
buttonaddTarget:selfaction:@selector(btn)forControlEvents:UIControlEventTouchUpInside];

   
   
UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap:)];
    tap.
delegate= self;

    [back
addGestureRecognizer:tap];
    [back
addSubview:imageView];
    [back
addSubview:label];
    [back
addSubview:button];
}
- (
void)btn
{
   
NSLog(@"button");
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch
{
   
// 取消labelimageview响应手势
   
if ([touch.viewisKindOfClass:[UILabelclass]] || touch.view== imageView)
    {
       
return NO;
    }
   
return YES;

}
- (
void)tap:(UITapGestureRecognizer*)tap
{
   
NSLog(@"tap.view = %@",tap.view);
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值