UISwipeGestureRecognizer is a discrete gesture as the Apple documentation said, so you need to use a continuous gesture, in this case use UIPanGestureRecognizer.
Here is the code:
- (void)viewDidLoad{
[super viewDidLoad];
// add pan recognizer to the view when initialized
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognized:)];
[panRecognizer setDelegate:self];
[yourView addGestureRecognizer:panRecognizer]; // add to the view you want to detect swipe on
}
-(void)panRecognized:(UIPanGestureRecognizer *)sender{
CGPoint distance = [sender translationInView: yourView];
if (sender.state == UIGestureRecognizerStateEnded) {
[sender cancelsTouchesInView];
if (distance.x > 70 && distance.y > -50 && distance.y < 50) { // right
NSLog(@"user swiped right");
NSLog(@"distance.x - %f", distance.x);
} else if (distance.x < -70 && distance.y > -50 && distance.y < 50) { //left
NSLog(@"user swiped left");
NSLog(@"distance.x - %f", distance.x);
}
if (distance.y > 0) { // down
NSLog(@"user swiped down");
NSLog(@"distance.y - %f", distance.y);
} else if (distance.y < 0) { //up
NSLog(@"user swiped up");
NSLog(@"distance.y - %f", distance.y);
}
}
}
Don't forget to add UIGestureRecognizerDelegate.