Domanda

In viewDidLoad I set:

    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [swipeGesture setDirection:(UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)];
    [self.view addGestureRecognizer:swipeGesture];

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"Swipe received.");
    UISwipeGestureRecognizerDirection temp = recognizer.direction;
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        [self backCalendarPressed:nil];
    }
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
    {
        [self nextCalendarPressed:nil];
    }
}

but recognizer.direction is always equal to '3'. And that's why I can't determine if it's left or right swipe.

È stato utile?

Soluzione

you have to set a separate gesture recognizer for each direction if you want to discern between left and right gestures. the direction property only gives you what you set as allowed direction (3 = both directions). You can give the same target method to both, and in that method ask for the direction of the recognizer as you are doing.

Altri suggerimenti

You should try this. It is working fine.

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(backCalendarPressed:)];
            [swipeLeft setDirection: UISwipeGestureRecognizerDirectionLeft ];
            [self.view addGestureRecognizer:swipeLeft];
            swipeLeft=nil;

            UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextCalendarPressed:)];
            [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
            [self.view addGestureRecognizer:swipeRight];

Basically you cannot set multiple directions to a single Swipe gesture recognizer. Use separate gesture recognizers for Left and Right directions.

try this:

The direction property only defines the allowed directions that are recognized as swipes, not the actual direction of a particular swipe.

The easiest would be to use two separate gesture recognizers instead.

If you want to capture swipes left and right that you can differentiate between, you'll have to set up a separate recognizer for each. Apple does this in their Simple Gesture Recognizers

UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromLeft:)];
[swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:swipeGestureLeft];

UISwipeGestureRecognizer *swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromRight:)];
[swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeGestureRight];



-(void)handleSwipeFromLeft:(UISwipeGestureRecognizer *)recognizer {
    [self backCalendarPressed:nil];
}

-(void)handleSwipeFromRight:(UISwipeGestureRecognizer *)recognizer {
    [self nextCalendarPressed:nil];
}

You should implement all the UIGestureRecognizer states in this handler method. Thus you can handle all the cases. I am stating this because I suspect the gesture recognizer has not yet properly recognized it's gesture. Try looking for the direction in the UIGestureRecognizerStateChanged case.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top