2010-12-15 150 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:我的英语不太好,所以如果有任何句子错误,修正会很高兴:)