我有parentView
和subView childView
。childView
位于我parentView
的中间,大约是它的一半大小。
我想在用户点击childView
时关闭parentView
。
打开UITapGestureRecognizer
后,以下代码将在parentView
中创建一个childView
。
我的问题是,当用户触摸任何视图而不仅仅是parentView
时,都会触发轻击事件。
因此,我想知道如果仅触摸了parentView,或者如果触摸了父视图,任何其他可能关闭子视图的方式如何使事件发生。
- (IBAction)selectRoutine:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
createRoutinePopupViewController* popupController = [storyboard instantiateViewControllerWithIdentifier:@"createRoutinePopupView"];
popupController.view.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
_ass = popupController;
//Tell the operating system the CreateRoutine view controller
//is becoming a child:
[self addChildViewController:popupController];
//add the target frame to self's view:
[self.view addSubview:popupController.view];
//Tell the operating system the view controller has moved:
[popupController didMoveToParentViewController:self];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:singleTap];
}
-(void) handleSingleTap: (id) sender {
NSLog(@"TEST STRING");
}
最佳答案
您需要使用UIGestureRecognizerDelegate,实现以下方法。
它将检查触摸的视图。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if( [touch view] != popupController.view)
return YES;
return NO;
}
关于ios - 触摸父 View 时关闭 subview ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16374684/