Failed to configure UISwipeGestureRecognizer correctly

I am writing code to move two fingers up or down to change status. Code as below:

UISwipeGestureRecognizer *aSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; [aSwipeGesture setDirection:UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown]; aSwipeGesture.numberOfTouchesRequired = 2; [self.View addGestureRecognizer:aSwipeGesture]; - (void)swipeGesture:(UISwipeGestureRecognizer *)sender { NSLog(@"Swipe received."); if (sender.direction==UISwipeGestureRecognizerDirectionUp) { NSLog(@"swipe up"); } else if (sender.direction==UISwipeGestureRecognizerDirectionDown) { NSLog(@"swipe down"); } } 

However, the only print magazine I could get was Swipe, obtained as shown below. I couldn’t get a message to scroll or scroll down, did I miss something? Thanks

 ViewController.m:228 Swipe received. ViewController.m:228 Swipe received. ViewController.m:228 Swipe received. ViewController.m:228 Swipe received. ViewController.m:228 Swipe received. ViewController.m:228 Swipe received. 

Updated: I have to use two fingers to complete the swipe action.

+2
ios uigesturerecognizer uiswipegesturerecognizer
Apr 24 '13 at 6:07
source share
5 answers

try it

 UISwipeGestureRecognizer *aSwipeGestureUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; [aSwipeGesture setDirection:UISwipeGestureRecognizerDirectionUp]; aSwipeGestureUp.numberOfTouchesRequired = 2; [self.View addGestureRecognizer:aSwipeGestureUp]; UISwipeGestureRecognizer *aSwipeGestureDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; [aSwipeGestureDown setDirection:UISwipeGestureRecognizerDirectionDown]; aSwipeGestureDown.numberOfTouchesRequired = 2; [self.View addGestureRecognizer:aSwipeGestureDown]; 
+6
Apr 24 '13 at 6:27
source share

Unfortunately, the API does not make any sense; you can install UISwipeGestureRecognizer to recognize napkins in any of 4 directions, but it will not tell you in which direction the napkins occurred.

they were careful enough to allow 4 constants to be used together (this is a bitmap), but the API is just broken.

you will need to use one recognizer for each direction, shame!

+5
Sep 17 '13 at 17:04 on
source share

In your case, you set the swipe direction as UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown. Thus, in the delegate callback, you will get the same value for sender.direction, so no logs are printed because the enumeration value does not match the sender.direction value.

If you want to separately handle up and down scrolling, you need to create two swipe gestures with up and down scroll directions and add to your view. Then, depending on the direction of the sender, complete the task.

+2
Apr 24 '13 at 6:28
source share

UISwipeGestureRecognizer * Updown = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @selector (handleGestureNext :)]; Updown.delegate = independently; [Updown setDirection: UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp]; [overLayView addGestureRecognizer: Updown];

  UISwipeGestureRecognizer *LeftRight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleGestureNext:)]; LeftRight.delegate=self; [LeftRight setDirection:UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight]; [overLayView addGestureRecognizer:LeftRight]; overLayView.userInteractionEnabled=NO; -(void)handleGestureNext:(UISwipeGestureRecognizer *)recognizer { NSLog(@"Swipe Recevied"); //Left //Right //Top //Bottom } 
+1
Sep 22 '14 at 12:25
source share

remove aSwipeGesture.numberOfTouchesRequired = 2; and try

-3
Apr 24 '13 at 6:09
source share



All Articles