2013-05-04 96 views
0

我有parentView和子視圖childViewchildView位於我的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"); 
} 

回答

2

您需要使用UIGestureRecognizerDelegate,實現以下方法。

它會檢查被觸摸的視圖。

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

    return NO; 
} 
+0

它說objView是undefined? – 2013-05-04 13:30:42

+0

顯然它應該說,因爲你需要替換它的父視圖的對象 – HarshIT 2013-05-04 13:31:57

+0

希望你在頭文件中添加了並且還分配了singleTap.delegate = self; – HarshIT 2013-05-04 13:34:18

相關問題