Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding swipe gesture recognizer to DetailViewContoller

Tags:

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; } 
like image 465
darko_5 Avatar asked Mar 01 '13 19:03

darko_5


People also ask

How to add swipe gesture in UIView in swift?

Fire up Xcode and create a blank project by choosing the App template from the iOS > Application section. Name the project Swipes, set Interface to Storyboard, and Language to Swift. Open ViewController. swift and declare a private, constant property with name swipeableView of type UIView .

How do I add swipe gestures in iOS?

Add four swipe gesture recognizers to your view. Set each one with the target direction from the attribute inspector. You can select right, left, up or down. One by one, select the swipe gesture recognizer, control + drag to your view controller.

What is Pan gesture?

A pan gesture occurs any time the user moves one or more fingers around the screen. A screen-edge pan gesture is a specialized pan gesture that originates from the edge of the screen. Use the UIPanGestureRecognizer class for pan gestures and the UIScreenEdgePanGestureRecognizer class for screen-edge pan gestures.


2 Answers

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."); } 
like image 85
nsgulliver Avatar answered Oct 17 '22 07:10

nsgulliver


- (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];         }];     } } 
like image 21
Jagat Dave Avatar answered Oct 17 '22 08:10

Jagat Dave