当父视图被触摸时关闭子视图
问题描述:
我有parentView
和子视图childView
。 childView
位于我的parentView
的中间,大小只有它的一半。如果用户点击parentView
,我想关闭childView
。当父视图被触摸时关闭子视图
一旦childView
处于打开状态,我的代码将在parentView
中创建一个UITapGestureRecognizer
。
我的问题是,当用户触摸任何视图时触发轻击事件,而不仅仅是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;
}
它说objView是undefined? – 2013-05-04 13:30:42
显然它应该说,因为你需要替换它的父视图的对象 – HarshIT 2013-05-04 13:31:57
希望你在头文件中添加了并且还分配了singleTap.delegate = self; –
HarshIT
2013-05-04 13:34:18