22
votes

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;
}
5

5 Answers

38
votes

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.");
}
19
votes
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizer:)];
    recognizer.direction = UISwipeGestureRecognizerDirectionRight;
    recognizer.delegate = self;
    [self.view addGestureRecognizer:recognizer];
}

- (void)swipeRecognizer:(UISwipeGestureRecognizer *)sender {
    if (sender.direction == UISwipeGestureRecognizerDirectionRight){
        [UIView animateWithDuration:0.3 animations:^{
            CGPoint Position = CGPointMake(self.view.frame.origin.x + 100.0, self.view.frame.origin.y);
            self.view.frame = CGRectMake(Position.x , Position.y , self.view.frame.size.width, self.view.frame.size.height);
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }
}
10
votes

Try This

//Right Swipe

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

//Left Swipe

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

call method

-(void)swipeHandlerRight:(id)sender
{
    //Your ViewController
}

-(void)swipeHandlerLeft:(id)sender
{
 //Your ViewController
}
0
votes

This works. It pushes the view controller from the navigationController.

- (void)viewDidLoad
{
     [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
     UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer           alloc] initWithTarget:self  action:@selector(swipeHandler:)];
     [self.view addGestureRecognizer:gestureRecognizer];
}

-(IBAction)swipeHandler:(UISwipeGestureRecognizer *)sender
{
      NSLog(@"SWIPE");
      UIViewController *tb = [[DetailViewController alloc] init];
      tb = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
      [self.navigationController pushViewController: tb animated:YES];
}

Then be sure to go to storyboard (or you could also do this manually) - click on the Detail View Controller and give the View controller the Identity: DetailViewController

0
votes

swift 4.0

Step 1: Add swipe Gesture(s) in viewDidLoad() method.

override func viewDidLoad() {
    super.viewDidLoad()

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
    swipeLeft.direction = .left
    self.view.addGestureRecognizer(swipeLeft)

    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
    swipeRight.direction = .right
    self.view.addGestureRecognizer(swipeRight)


}

Step 2: Check the gesture detection in handleGesture() method

@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
    if gesture.direction == UISwipeGestureRecognizerDirection.right {
        print("Swipe Right")
    }
    else if gesture.direction == UISwipeGestureRecognizerDirection.left {
        print("Swipe Left")
    }

}