Kristen Martinson
Kristen Martinson

Reputation: 1869

UIView: touches: swiping?

Is there any easier way of detecting a swipe motion and its direction without doing touchesBegin and touchesEnd?

thanks

Upvotes: 1

Views: 267

Answers (2)

Savas Adar
Savas Adar

Reputation: 4262

This is working only one direction. You should use like this for 2 direction.

    UISwipeGestureRecognizer *swipeRecognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiped:)];
swipeRecognizerLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[_cameraView addGestureRecognizer:swipeRecognizerLeft];

UISwipeGestureRecognizer *swipeRecognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiped:)];
swipeRecognizerRight.direction = UISwipeGestureRecognizerDirectionRight;
[_cameraView addGestureRecognizer:swipeRecognizerRight];

Upvotes: 0

Paul.s
Paul.s

Reputation: 38728

How about using a UISwipeGestureRecognizer? You create the UISwipeGestureRecognizer and then add it to your view check out the docs UISwipeGestureRecognizer.

Setting it up may look like this:

UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiped:)];
[myView addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release]; swipeRecognizer = nil;

Then implement your method that will be called when swiped (this is the @selector(swiped:))

- (void)swiped:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
    if (swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"Direction = right");
    } else if (swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"Direction = left");
    }
}

Upvotes: 4

Related Questions