2016-05-17 72 views
1

enter image description here叩擊手勢子視圖觸摸檢測

我創造抽屜

self.isShowMenuVC = NO; 
_menuView = [MenuViewController viewController]; 
[self.menuView setDelegate:self]; 
[self addChildViewController:self.menuView]; 
[self.menuView.view setFrame:CGRectMake(-kMenuTableWidth, 0, kMenuTableWidth, self.view.frame.size.height)]; 
[self.view addSubview:self.menuView.view]; 
[self.menuView didMoveToParentViewController:self]; 

UITapGestureRecognizer *outsideTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(outsideTapped:)]; 
[self.view addGestureRecognizer:outsideTap]; 
outsideTap.delegate = self; 

,當按鈕自來水我剛纔設置的_menuView.view幀像一個抽屜

我想是來檢測觸摸外面的抽屜,但我不能這樣做

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { 
if (touch.view == self.menuView.view) { 
    NSLog(@"Touch Drawer"); 
} else { 
    NSLog(@"Touch Outside"); 
} 

return YES; 
} 

但它始終顯示觸摸外」

我思念的東西,但不知道該怎麼感謝事先

與2的姿勢也可以嘗試,但沒有工作,因爲在self.view一個手勢所以,當我點擊抽屜方法調用2次。

對於我試圖禁用一個手勢,仍然調用兩次

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { 

if (gestureRecognizer == self.touchInDrawer) { 
    NSLog(@"Touch in drawer"); 
    [self.touchOutSideDrawer setEnabled:NO]; 
} else { 
    NSLog(@"Outside"); 
    [self hideMenuView]; 
} 
return YES; 
} 

回答

0

我有解決這一問題增加了兩個手勢

@property (strong,nonatomic)  UITapGestureRecognizer   *touchInDrawer; 
@property (strong,nonatomic)  UITapGestureRecognizer   *touchOutSideDrawer; 

按@Eugene Zaychenko的回答:但仍然有問題,因爲委託方法調用兩次

也我不能[self.touchOutSideDrawer setEnabled:NO];當在抽屜裏輕觸,因爲在接觸後t將其從視圖中刪除,並不會再執行,如果[self.touchOutSideDrawer setEnabled:YES];

但最有趣的是

_touchInDrawer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(outsideTapped:)]; 
[self.menuView.view addGestureRecognizer:self.touchInDrawer]; 
self.touchInDrawer.delegate = self; 

_touchOutSideDrawer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(outsideTapped:)]; 
[self.view addGestureRecognizer:self.touchOutSideDrawer]; 
self.touchOutSideDrawer.delegate = self; 

outsideTapped方法僅調用一次,所以我轉移我的所有代碼,而正在

- (void) outsideTapped:(UITapGestureRecognizer *)gestureRecognizer { 

if (gestureRecognizer == self.touchOutSideDrawer) { 
    // [self.view removeGestureRecognizer:self.touchOutSideDrawer]; 
    [self hideMenuView]; 
    NSLog(@"Outside"); 

} else { 
    NSLog(@"Touch in drawer"); 

    // [self.touchOutSideDrawer setEnabled:NO]; 

} 

} 
1

的UITapGestureRecognizer不能檢測其所屬的視圖以外的觸感。

您需要創建另一個UITapGestureRecognizer並將它們添加到self.menuView.view。

你也可以做兩個@property你的手勢識別,並檢查他們的內部方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { 
if (gestureRecognizer == self.firstGesture) { 
    NSLog(@"Touch in first gesture"); 
} else { 
    NSLog(@"Touch in another gesture"); 
} 
+0

我試過不工作,因爲我在self.view中添加了手勢,所以當我點擊抽屜手勢委託方法觸發2次 –

+0

嘗試實現委託的方法: - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeShultaneouslyWithGestureRecognizer :(UIGestureRecognizer *)otherGestureRecognizer 並且僅僅返回NO; –

+0

也嘗試實施方法: - (BOOL)gestureRecognizerShouldBegin :(UIGestureRecognizer *)gestureRecognizer –