开发者

iPhone simulator swipe right works, but swipe left does not?

开发者 https://www.devze.com 2023-03-17 12:12 出处:网络
I\'m trying to use swipe left and right on a UIScrollView. However it looks like swipe left does not work in iPhone simulator even though swipe right does. Did I miss any step?

I'm trying to use swipe left and right on a UIScrollView. However it looks like swipe left does not work in iPhone simulator even though swipe right does. Did I miss any step?

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    self.scrollView.multipleTouchEnabled = YES;
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    swipe.delaysTouchesBegan = YES;
    swipe.numberOfTouchesRequired = 2;
    [self.scrollView addGestureRecognizer:swipe];
    [swipe release];
}

- (void)handleSwipe:(UISwipeGestureRecogniz开发者_JAVA技巧er *)recognizer
{
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {

    } else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {

    }
}


Use Following:

UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
[mainSlideShowImageScrollView addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[mainSlideShowImageScrollView addGestureRecognizer:leftRecognizer];
[leftRecognizer release];   



- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
   {
      //Do moving
   }

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
{
  // do moving
}


Your 'handleSwipe' code is actually correct. You need two UISwipeGestureRecognizers but you can point them both to the same handler, containing your 'IF' statement.


You can create one gesture recognizer that handles both left and right swipe (or even all directions-!):

    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
        swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;

All directions:

  swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号