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

// 父类 view
   
UIImageView *back = [[ UIImageView alloc ] initWithFrame : CGRectMake ( 0 , 0 , kScreenWidth , kScreenHeight )];
    back.
image = [ UIImage imageNamed : @"Default-568h@2x" ];
    back.
userInteractionEnabled = YES ;
    [
self . view addSubview :back];
   
   
// 父类 view 中的 imageView
   
imageView = [[ UIImageView alloc ] initWithFrame : CGRectMake ( 100 , 100 , 100 , 100 )];
   
imageView . image = [ UIImage imageNamed : @"Default-568h@2x" ];
   
imageView . userInteractionEnabled = YES ;
   
   
// 父类 view 中的 label
   
label = [[ UILabel alloc ] initWithFrame : CGRectMake ( 100 , 240 , 100 , 100 )];
   
label . text = @"label" ;
   
label . backgroundColor = [ UIColor grayColor ];
   
// 此属性必须打开 , 不然 UITouch 捕获 Label
   
label . userInteractionEnabled = YES ;
   
   
// 父类 view 中的 button
   
button = [ UIButton buttonWithType : UIButtonTypeCustom ];
    [
button setTitle : @"button" forState : UIControlStateNormal ];
   
button . frame = CGRectMake ( 100 , 350 , 100 , 100 );
   
button . backgroundColor = [ UIColor purpleColor ];
    [
button addTarget : self action : @selector (btn) forControlEvents : UIControlEventTouchUpInside ];

   
   
UITapGestureRecognizer *tap = [[ UITapGestureRecognizer alloc ] initWithTarget : self action : @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
{
   
// 取消 label imageview 响应手势
   
if ([touch. view isKindOfClass :[ UILabel class ]] || touch. view == imageView )
    {
       
return NO ;
    }
   
return YES ;

}
- (
void )tap:( UITapGestureRecognizer *)tap
{
   
NSLog ( @"tap.view = %@" ,tap. view );
}

你可能感兴趣的:(iOS-基础,OC)