3

I'm writing code to move my two fingers up or down on a view to change some status. The 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 log I could receive was Swipe received as below shows. I couldn't get the message for swipe up or swipe down, did I miss anything? 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 finish the swipe action.

Sonny Parlin
  • 51
  • 2
  • 7
  • possible duplicate of [Setting direction for UISwipeGestureRecognizer](http://stackoverflow.com/questions/3319209/setting-direction-for-uiswipegesturerecognizer) – Rui Peres Apr 24 '13 at 06:14
  • @JackyBoy: No my problem is about the direction, and I could receive NSLog(@"Swipe received."), but the NSLog(@"swipe up"); and NSLog(@"swipe down"); – Sonny Parlin Apr 24 '13 at 06:19
  • print sender.direction – AMohan Apr 24 '13 at 06:30

5 Answers5

6

Try this

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];
5

sadly the API makes no sense at all, you can SET a UISwipeGestureRecognizer to recognize a swipe in any of the 4 directions, but it will not tell you what direction the swipe happened in.

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

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

Pizzaiola Gorgonzola
  • 2,789
  • 1
  • 17
  • 12
2

In your case you have set Direction of swipe as UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown . So in delegate callback you will receive same value for sender.direction, that is why the logs are not printed as the enum value is not matching with sender.direction value.

If you want to handle separately the up and down swipe, you need to create 2 swipe gestures with up and down swipe directions and add to your view. Then based on the direction of sender do the task.

Amit
  • 1,043
  • 1
  • 10
  • 32
2

UISwipeGestureRecognizer *Updown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleGestureNext:)]; Updown.delegate=self; [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
}
bhavik
  • 1,673
  • 2
  • 14
  • 20
-3

remove aSwipeGesture.numberOfTouchesRequired = 2; and try

Navn
  • 176
  • 8