2

I have a UIView demoView in a UIScrollView. I want to scroll the UIScroll view vertically and don't scroll horizontally. I add UISwipeGestureRecognizer to the UIView. The code is (self here is the 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];

The result is I can only detect the left swipe gesture, can't detect right swipe gesture. What's wrong with my code? Thank you.

Mundi
  • 79,884
  • 17
  • 117
  • 140
Vigor
  • 1,706
  • 3
  • 26
  • 47
  • Maybe `self` here is the view controller? – Mundi Sep 04 '12 at 13:18
  • self here is a UIScrollView's subclass. I use a .xib file. this subclass is bundled with the .xib. – Vigor Sep 04 '12 at 13:21
  • Why are do add swipe recognizers at all? If you don't want your scroll view to scroll horizontally you should specify contentSize with 0 width (or less then viewport width). – average dev Sep 04 '12 at 13:31

2 Answers2

1

The default value of UISwipeGestureRecognizer direction property is UISwipeGestureRecognizerDirectionRight so you are creating two recognizers that does the same thing.

This is how i would solve the problem:

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];
}
Martin_G
  • 125
  • 1
  • 5
0

To detect a swipe into a UIScrollView or a UIWebView is hard. You can use "Swipe4ScrollViews" from here: UISwipeGestureRecognizer not working

Community
  • 1
  • 1
Joze
  • 668
  • 7
  • 13