garethdn
garethdn

Reputation: 12351

Unusual behavior with UISwipeGestureRecognizer

I'm getting some unusual behavior with UISwipeGestureRecognizer. The first swipe is detected correctly but the second swipe is not. Then the third swipe is detected correctly but the fourth is not etc. I have two swipe gesture recognizers, one for left and one for right. Here is the code:

.h file

- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer;

.m file

- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer {
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"you swiped left");
        [self spinSunCounterClockwise];
    } else {
        NSLog(@"you swiped right");
        [self spinSunClockwise];
    }

}

So, for example, if i swipe left the first time, spinSunCounterClockwise is called, but if i swipe left again, spinSunClockwise is called.

Another example is, if i swipe left the first time, spinSunCounterClockwise is called. If i swipe right, spinSunCounterClockwise is called again.

Any help please?

EDIT 1: One of the spinning methods

-(void) spinSunCounterClockwise
{
    [UIView animateWithDuration:2.0f animations:^{
        imgSun.transform = CGAffineTransformRotate(imgSun.transform, 3.142);
    } completion:^(BOOL finished) {

    }];
}

Upvotes: 0

Views: 194

Answers (1)

omz
omz

Reputation: 53551

When you rotate your view by 180° (btw, you should use M_PI instead of 3.142), the view is upside down afterwards, so what was previously left is now right and vice-versa.

So when you swipe left the first time, the second time a left swipe (from your perspective) is actually recognized as a right swipe, while a right swipe is actually a left swipe, which explains the the behavior you've observed.

After two gestures (360° rotation), your view is in its original orientation again, so the recognition is "normal" again for the third gesture.

Upvotes: 1

Related Questions