2012-02-19 53 views
9

我在我的iOS應用程序中使用UIGestureRecognizer,我遇到了一些問題。UIGestureRecognizer部分UIView

我只想讓手勢在視圖的某個區域工作,所以我創建了一個具有特定框架的新UIView並將其添加到根視圖。這些手勢正常工作,但現在唯一的問題是,我不能點擊新視圖之下/之後的東西(位於根視圖上的對象)。如果我將userInteractionEnabled設置爲NO,它會打破手勢,因此這不是一個選項。

我能做些什麼來解決這個問題?

謝謝。

回答

31

不要爲您的手勢識別器創建新視圖。識別器實現了一個locationInView:方法。爲包含敏感區域的視圖進行設置。在handleGesture上,點擊測試你關心的區域,如下所示:

0)在包含您關心的區域的視圖上執行所有操作。不要僅爲手勢識別器添加特殊視圖。

1)安裝mySensitiveRect

@property (assign, nonatomic) CGRect mySensitiveRect; 
@synthesize mySensitiveRect=_mySensitiveRect; 
self.mySensitiveRect = CGRectMake(0.0, 240.0, 320.0, 240.0); 

2)創建gestureRecognizer:

gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; 
[self.view addGestureRecognizer:gr]; 
// if not using ARC, you should [gr release]; 
// mySensitiveRect coords are in the coordinate system of self.view 


- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer { 
    CGPoint p = [gestureRecognizer locationInView:self.view]; 
    if (CGRectContainsPoint(mySensitiveRect, p)) { 
     NSLog(@"got a tap in the region i care about"); 
    } else { 
     NSLog(@"got a tap, but not where i need it"); 
    } 
} 

敏感RECT應該在MyView的座標系統進行初始化,同樣的看法,對此你附加的識別。

+0

對不起,這會怎麼做?併爲mySensitiveRect變量,我使用(0,0,320,480)?你有完整的例子嗎?感謝:D – 2012-02-19 12:18:13

+0

@DavidMurray:希望這更清晰 – danh 2012-02-19 18:37:17

+0

謝謝,這使它。 :-) – 2012-02-20 13:04:34