gpt4 book ai didi

ios - 触摸父 View 时关闭 subview

转载 作者:行者123 更新时间:2023-11-28 22:34:53 25 4
gpt4 key购买 nike

我有 parentView 和 subView childViewchildView 位于我的 parentView 的中间,大约是它的一半大小。

我想在用户点击 parentView 时关闭 childView

一旦 childView 打开,我的如下代码会在 parentView 中创建一个 UITapGestureRecognizer

我的问题是,当用户触摸任何 View 时都会触发点击事件,而不仅仅是 parentView

因此,我想知道如果仅触摸 parentView 或任何其他可能在触摸父 View 时关闭 subview ,我怎样才能使事件发生。

- (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 ,实现下面的方法。

它将检查触摸的 View 。

- (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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com