在UIScrollView中使用UISwipeGestureRecognizer,无法检测到右键滑动,只检测左侧滑动

时间:2012-09-04 13:13:05

标签: objective-c uiscrollview uiswipegesturerecognizer

我在UIScrollView中有一个UIView demoView 。我想垂直滚动UIScroll视图,不要水平滚动。我将UISwipeGestureRecognizer添加到UIView中。代码是(这里是自己的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];

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

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)

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