I’m trying to implement in my ViewController( which has a tableView) two gesture recognisers that have to work one after another. The first one is a swipe down gesture and the second a long press gesture.
Here’s my code modified with @sergio suggestions
- (void)viewDidLoad
{
[super viewDidLoad];
swipeDown = [[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDownAction)] autorelease];
longPress = [[[CustomLongPress alloc]initWithTarget:self action:@selector(longPressAction)] autorelease];
longPress.minimumPressDuration = 2;
swipeDown.numberOfTouchesRequired = 1;
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
swipeDown.delegate = self ;
longPress.delegate = self ;
[myTableView addGestureRecognizer:swipeDown];
[myTableView addGestureRecognizer:longPress];
}
-(void)swipeDownAction {
_methodHasBeenCalled = YES; // bool @property declared in .h
NSLog(@"Swipe down detected");
}
-(void)longPressAction {
NSLog(@"long press detected");
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
And my UILongPressGestureRecognizer subclass:
#import "CustomLongPress.h"
#import "ViewController.h"
@interface CustomLongPress()
{
ViewController *vc;
}
@end
@implementation CustomLongPress
-(id)initWithTarget:(id)target action:(SEL)action controller:(ViewController *)viewCon
{
self = [super initWithTarget:target action:action];
if (self) {
vc = viewCon;
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(vc.methodHasBeenCalled ? @"Yes" : @"No");
if (vc.methodHasBeenCalled) {
[super touchesBegan:touches withEvent:event];
}
}
Unfortunately, I still get only the log from swipeDown but no log when it comes to longPress
For that you need to create you own custom gesture recognizer. The best way would be for you to subclass
UILongPressGestureRecognizerand make it “accept” the long press only after the swipe has ended. E.g., in thetouchesBeganmethodIn this way, both gesture recognizers would try to recognize the gesture: the swipe recognizer would do so immediately; while the custom recognizer would “wait” for the swipe recognizer to have fired.
An easy way to implement the condition
<if swipe recognizer action method has been called>would be setting a global flag in your swipe action (you set it when the swipe action is executed; then the custom recognizer reads its value). This is easy, by far not the best implementation you can find.Another approach would be relying on the
requiresGestureRecognizerToFailto chain 3 standard gesture recognizers, let’s call them A, B, and C, where:UISwipeGestureRecognizer;UILongPressGestureRecognizer;You configure them like this:
whereby
x --> ydenotes thatxrequiresyto fail. Thus, you would have thatC(your long press gesture recognizer) will require theBGR to fail andBrequiresA(your swipe recognizer) to fail;Bwill fail as soon asAwill recognized the swipe; onceBfails,Cwill be allowed to recognize the long press if any.EDIT:
after reading your comment, would you mind trying this and see if it helps out:
remove your overridden
touchesBeganand replace it by:then define:
If this does not work, then make your custom gesture recognizer inherit from a generic gesture recognizer and
leave
touchesBegan:in place and replacetouchesMovedwith:and add:
You will need to define in
.h