Oli Black
Oli Black

Reputation: 441

Swiping ios gesture

I am trying to create a simple app where the user can swipe left and then right while keeping there finger on the screen. I want to count how many total swipes they do including change of direction. I am using the uiswipegesture with direction but it only called the action when it is a new swipe. To make more sense, its almost testing how many time the user can move their finger left to right and back again within a certain time frame. At the moment I have these kind of methods inside my viewdidload

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

and then this as the action

-(void) oneFingerSwipeLeft:(UIGestureRecognizer*)recognizer {
NSLog(@"user swipped left");
}

Any help would be appreciated.

Upvotes: 0

Views: 444

Answers (1)

Joel
Joel

Reputation: 16124

I would do something like this.

Setup some variables to store what you are tracking:

@property (nonatomic) int swipeCount;
@property (nonatomic) CGPoint previousLocation;

Create a UIPanGestureRecognizer:

UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizePanWithGestureRecognizer:)];
[gesture setDelegate:self];
[self.view addGestureRecognizer:gesture];

Handle the callback

- (void)didRecognizePanWithGestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer
{
    switch (gestureRecognizer.state)
    {
        case UIGestureRecognizerStateBegan:
            [self handleGestureBeganWithRecognizer:gestureRecognizer];
            break;

        case UIGestureRecognizerStateChanged:
            [self handleGestureChangedWithRecognizer:gestureRecognizer];
            break;

        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:
            [self handleGestureEndedWithRecognizer:gestureRecognizer];
            break;

        default:
            break;
    }
}

Track the info you're looking to capture while the user swipes back and forth

- (void)handleGestureBeganWithRecognizer:(UIPanGestureRecognizer *)gestureRecognizer
{
    [self setSwipeCount:0];
    [self setPreviousTouchLocation:[gestureRecognizer locationInView:self.view]];
}

- (void)handleGestureChangedWithRecognizer:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGPoint currentTouchLocation = [gestureRecognizer locationInView:self.view];
    CGFloat delta = currentTouchLocation.x - self.previousTouchLocation.x;
    [self setPreviousTouchLocation:currentTouchLocation];

    //... figure out if they changed directions based on delta positive or negative

}

- (void)handleGestureEndedWithRecognizer:(UIPanGestureRecognizer *)gestureRecognizer
{
    //.... finish up
}

Upvotes: 1

Related Questions