首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >手势识别器和TableView

手势识别器和TableView
EN

Stack Overflow用户
提问于 2010-12-16 05:10:58
回答 3查看 27.4K关注 0票数 13

我有一个覆盖所有UITableView的UIView。UIView使用手势识别器来控制表格显示的内容。我仍然需要垂直的UITableView滚动和行点击。如何将这些信息从手势识别器传递到表中?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-12-17 00:59:58

将你的手势分配给表视图,表会处理它:

代码语言:javascript
运行
复制
UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc]
        initWithTarget:self action:@selector(handleSwipeFrom:)];
[gesture setDirection:
        (UISwipeGestureRecognizerDirectionLeft
        |UISwipeGestureRecognizerDirectionRight)];
[tableView addGestureRecognizer:gesture];
[gesture release];

然后在你的手势动作方法中,根据方向采取行动:

代码语言:javascript
运行
复制
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        [self moveLeftColumnButtonPressed:nil];
    }
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
        [self moveRightColumnButtonPressed:nil];
    }
}

在内部处理之后,该表将只传递您所请求的手势。

票数 30
EN

Stack Overflow用户

发布于 2012-01-22 02:17:21

如果您需要知道手机的indexPath:

代码语言:javascript
运行
复制
- (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中已经回答过了。

票数 31
EN

Stack Overflow用户

发布于 2012-03-28 15:35:52

我尝试了Rob Bonner的建议,它很有效。谢谢。

但是,在我的例子中,方向识别有一个问题。(recognizer.direction总是引用3)我使用的是IOS5 SDK和xcode4。

这似乎是由“手势setDirection:(左|右)”引起的,我想。(因为预定义的(dir left | dir right)计算结果是3)

因此,如果有人像我一样有问题,想要分别识别向左和向右滑动,那么将两个识别器分配给不同方向的表视图。

如下所示:

代码语言:javascript
运行
复制
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];

和下面的手势动作:

代码语言:javascript
运行
复制
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
    [self moveLeftColumnButtonPressed:nil];
}

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

我用ARC功能编码,如果你不使用ARC,添加发布代码。

附言:我的英语不是很好,所以如果有任何句子错误,改正会很高兴:)

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4454920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档