0

我有parentView和 subView childViewchildView位于我的中间,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");
}
4

1 回答 1

2

您需要使用 UIGestureRecognizerDelegate ,实现以下方法。

它将检查触摸的视图。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if( [touch view] != popupController.view)
        return YES;

    return NO;
}
于 2013-05-04T13:28:17.643 回答