调用触摸手势事件时如何检查其父视图或子视图?
Posted
技术标签:
【中文标题】调用触摸手势事件时如何检查其父视图或子视图?【英文标题】:How can I check whether its parent view or subview when touch gesture event called? 【发布时间】:2016-10-01 05:37:04 【问题描述】:我有两种看法。一种是包含子视图的透明视图。我只想在单击父视图时删除screenView
。当我单击popUpView
时,我不想调用tapGesture。怎么查?
screenView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
screenView.backgroundColor = [UIColor clearColor];
UITapGestureRecognizer * clearTable = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clearTableViewAction:)];
[clearTable setNumberOfTapsRequired:1];
[screenView setUserInteractionEnabled:YES];
[screenView addGestureRecognizer:clearTable];
screenView.tag = 100;
[self.view addSubview:screenView];
self.popUpView = [[UIView alloc]init];
self.popUpView.frame = ...
self.popUpView.backgroundColor = WhiteColor;
self.popUpView.userInteractionEnabled = YES;
self.popUpView.tag = 200;
[screenView addSubview:self.popUpView];
-(void)clearTableViewAction:(UITapGestureRecognizer*)sender
if(sender.view.tag == 100)
[UIView animateWithDuration:0.2
animations:^screenView.alpha = 0.0;
completion:^(BOOL finished) [screenView removeFromSuperview];
];
【问题讨论】:
如果将 userInteractionEnabled 设为 NO。 TapGesture 仍然被调用。 【参考方案1】:使用shouldReceiveTouch
委托方法并检查:
喜欢,
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
if ([touch.view.tag == 100)
return YES;
else
return NO;
现在,如果您点击popUpView
,则不会调用clearTableViewAction
手势方法。
【讨论】:
以上是关于调用触摸手势事件时如何检查其父视图或子视图?的主要内容,如果未能解决你的问题,请参考以下文章
如果 UIButton 的位置超出其父视图但它是可见的,它可以响应触摸事件吗?