[英]Close subview when the parent view is touched
I have parentView
and subView childView
. 我有
parentView
和subView childView
。 childView
is positioned in the middle of my parentView
and is about half it's size. childView
定位在我的中间parentView
,是大约一半的大小。
I want to close childView
when user tap on the parentView
. 我想在用户点击
childView
时关闭parentView
。
My code as follows creates a UITapGestureRecognizer
in the parentView
once the childView
is open. 如下我的代码创建一个
UITapGestureRecognizer
在parentView
一旦childView
是开放的。
My problem is that the tap event is triggered when the user touches any view, not just the parentView
. 我的问题是,当用户触摸任何视图时触发tap事件,而不仅仅是
parentView
。
Thus, I was wondering how can I just make the event happen if ONLY the parentView is touched or any other possible to close the sub view if the parent is touched. 因此,我想知道如果只触摸了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");
}
You need to use UIGestureRecognizerDelegate , implement following method. 您需要使用UIGestureRecognizerDelegate,实现以下方法。
it will check the touched view. 它会检查触摸的视图。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if( [touch view] != popupController.view)
return YES;
return NO;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.