//
父类
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" );
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 );
}
{
// 取消 label 和 imageview 响应手势
if ([touch. view isKindOfClass :[ UILabel class ]] || touch. view == imageView )
{
return NO ;
}
return YES ;
}
- ( void )tap:( UITapGestureRecognizer *)tap
{
NSLog ( @"tap.view = %@" ,tap. view );
}