2010-12-15 151 views
13

我有一個UIView覆蓋了所有的UITableView。 UIView使用手勢識別器來控制表格顯示的內容。 我仍然需要垂直UITableView滾動和行水龍頭。 如何從手勢識別器將這些傳遞到桌子上?手勢識別器和TableView

+0

我相信「[tablView手勢]」應爲「[yourTableView addGestureRecognizer:手勢]」 – 2011-06-23 19:17:15

回答

30

將您的姿態向表視圖,該表將照顧它:

UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] 
     initWithTarget:self action:@selector(handleSwipeFrom:)]; 
[gesture setDirection: 
     (UISwipeGestureRecognizerDirectionLeft 
     |UISwipeGestureRecognizerDirectionRight)]; 
[tableView addGestureRecognizer:gesture]; 
[gesture release]; 

然後在你的手勢操作方法,行爲基礎上的方向:

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer { 
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) { 
     [self moveLeftColumnButtonPressed:nil]; 
    } 
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) { 
     [self moveRightColumnButtonPressed:nil]; 
    } 
} 

該表將只有通過你在內部處理之後所要求的手勢。

+0

這是很好的答案。但是,你能告訴我如何知道handleSwipeFrom方法中的tableview indexPath嗎? – dinesh 2012-01-20 10:14:48

+4

這不起作用 - UISwipeGestureRecognizer上的direction屬性指示可以識別哪些方向(在本例中是左和右)而不是WAS滑動的方向。您需要分離手勢識別器 – 2013-09-02 10:22:12

+1

是的,recognitionizer.direction將始終爲3並且永不匹配。這可以說是一個錯誤:http://stackoverflow.com/questions/3319209/setting-direction-for-uiswipegesturerecognizer – Symmetric 2013-09-29 03:33:16

31

如果你需要知道你的電池的indexPath:

- (void)handleSwipeFrom:(UIGestureRecognizer *)recognizer { 
    CGPoint swipeLocation = [recognizer locationInView:self.tableView]; 
    NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation]; 
    UITableViewCell *swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath]; 
} 

UIGestureRecognizer and UITableViewCell issue以前回答。

+0

謝謝你,我一直在尋找這個解決方案。我沒有任何線索'locationInView'非常感謝 – carbonr 2012-01-30 08:46:54

+0

保存我的培根,謝謝! – jcrowson 2012-11-18 13:33:47

7

我試過Rob Bonner的建議,它的效果很好。謝謝。

但是,在我的情況下,方向識別存在問題。 (recognitionizer.direction總是指3)我使用IOS5 SDK和Xcode 4.

這似乎是由「[gesture setDirection:(left | right)]」引起的。 (因爲預定義的(dir left | dir right)計算結果是3)

因此,如果某人有像我這樣的問題,並且想要識別左右劃分,則將兩個識別器分配給不同方向的表視圖。

像這樣:

UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] 
              initWithTarget:self 
              action:@selector(handleSwipeLeft:)]; 
[swipeLeftGesture setDirection: UISwipeGestureRecognizerDirectionLeft]; 

UISwipeGestureRecognizer *swipeRightGesture = [[UISwipeGestureRecognizer alloc] 
               initWithTarget:self 
               action:@selector(handleSwipeRight:)]; 

[swipeRightGesture setDirection: UISwipeGestureRecognizerDirectionRight]; 

[tableView addGestureRecognizer:swipeLeftGesture]; 
[tableView addGestureRecognizer:swipeRightGesture]; 

和手勢動作如下:

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer { 
    [self moveLeftColumnButtonPressed:nil]; 
} 

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer { 
    [self moveRightColumnButtonPressed:nil]; 
} 

,如果你不使用ARC我與ARC功能再編碼,增​​加發行代碼。

PS:我的英語不太好,所以如果有任何句子錯誤,修正會很高興:)