触摸父视图时关闭子视图

时间:2013-05-04 13:15:22

标签: ios objective-c cocoa-touch

我有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");
}

1 个答案:

答案 0 :(得分:2)

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

它将检查触摸的视图。

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

    return NO;
}