我有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/

10-11 04:59