i need to detect direction of my swipe gesture and i've got problem with it. gesture is working, but i don't know how to detect direction. ...
swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(detectSwipe:)];
[swipeGesture setNumberOfTouchesRequired:1];
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp];
[appView addGestureRecognizer:swipeGesture];
-(void)detectSwipe:(UISwipeGestureRecognizer *)recognizer {
switch (recognizer.direction) {
case UISwipeGestureRecognizerDirectionUp:
NSLog(@"smth1");
break;
case UISwipeGestureRecognizerDirectionDown:
NSLog(@"smth2");
default:
break;
}
}
it's not working :/
Extending omz's solution:
self.myView
is the view I want to put the gesture recognizer on. The code below is not tested, I guess it would be better to keep the recognizers asproperty
s and add them insideviewDidLoad()
orxib
file.self
is aUIViewController
.Add those two methods to your
UIViewController
and add necessary actions:Here is an example from one of my projects:
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. You could also inspect the location of the touch when the gesture starts and when it ends with the
locationInView:
method.