2

我在 UIScrollView 中有一个 UIView demoView。我想垂直滚动 UIScroll 视图而不是水平滚动。我将 UISwipeGestureRecognizer 添加到 UIView。代码是(这里的self是UIScrollView):

self.demoView.userInteractionEnabled = YES;
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] 
    initWithTarget:self action:@selector(rightSwipeLineChart:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.demoView addGestureRecognizer:rightSwipe];

UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] 
    initWithTarget:self action:@selector(leftSwipeLineChart:)];
[self.demoView addGestureRecognizer:leftSwipe];

结果是我只能检测到左滑动手势,无法检测到右滑动手势。我的代码有什么问题?谢谢你。

4

2 回答 2

1

UISwipeGestureRecognizer 方向属性的默认值是 UISwipeGestureRecognizerDirectionRight,因此您正在创建两个执行相同操作的识别器。

这就是我解决问题的方法:

for (int i = 0; i < 2; i++) {
    UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onGestureSwipe:)];
    swipeRecognizer.direction = (i == 0 ? UISwipeGestureRecognizerDirectionRight : UISwipeGestureRecognizerDirectionLeft);
    [self.tableView addGestureRecognizer:swipeRecognizer];
}
于 2013-09-17T17:08:54.277 回答
0

很难检测到 UIScrollView 或 UIWebView 的滑动。您可以从这里使用“Swipe4ScrollViews”: UISwipeGestureRecognizer not working

于 2012-09-12T07:57:11.090 回答