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; }
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 .
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.
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.
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."); }
- (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]; }]; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With