Adding swipe gesture recognizer to DetailViewContoller

darko_5 picture darko_5 · Mar 1, 2013 · Viewed 42.8k times · Source

I have simple xcode project just made from "Master-Detail Application" template, for iPad. When device is in Portrait orientation, master view is hidden and when you swipe right on detail view, master view will show up. Now, i want to add right swipe gesture recognizer to detail view, like that:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self configureView];

    UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler)];
    [self.view addGestureRecognizer:gestureRecognizer];
}

-(void)swipeHandler{
    NSLog(@"SWIPE");
}

But this code causes that when i swipe on detail view, "SWIPE" log appears in console, but master view doesn't show up.

How to add right swipe gesture recognizer to detail view, so it wont prevent master view to show up and my handler for recognizer will work?

Thanks in advance.

EDIT. I want my right swipe recognizer handler to work simultaneously with this built in one, which shows up master view, but that following code isn't a solution for this specific situation:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    return YES;
}

Answer

nsgulliver picture nsgulliver · Mar 1, 2013

you should set the direction for the swipe in order to add the right swipe

UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler:)];
    [gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [self.view addGestureRecognizer:gestureRecognizer];

and your swipe handler might look like

-(void)swipeHandler:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"Swipe received.");
}