switching view controllers using swipe gestures
Asked Answered
O

6

8

Okay so here is the problem I'm running into:

I am attempting to switch from one viewController that I named MenuViewController which contains my menu (obviously). I have a separate viewController named ViewController that contains my mapView. I would like to be able to double finger swipe left from my MenuViewController over to my mapView.

I'm not exactly sure where to start.

Also, I am using xib files, and not the storyboard. Running iOS 6.

Ostrander answered 26/6, 2013 at 6:9 Comment(0)
H
12
UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
[self.view addGestureRecognizer:swipeLeftGesture];
swipeLeftGesture.direction=UISwipeGestureRecognizerDirectionLeft;

-(void)handleSwipeGesture:(UIGestureRecognizer *) sender
{
    NSUInteger touches = sender.numberOfTouches;
    if (touches == 2)
    {
        if (sender.state == UIGestureRecognizerStateEnded)
        { 
            //Add view controller here    
        }
    }  
}
Heehaw answered 26/6, 2013 at 6:15 Comment(4)
And this would go into my viewDidLoad correct? What goes into my header file? Also, how exactly would I "add view controller here"?Ostrander
put the swipe gesture code(first 3 lines in my ans) in viewDidLoad. Nothing in .h file. Allocate your view controller & just push, add or present it in handleSwipeGesture method.Heehaw
This is how I added my viewController to that line and it's not working: ViewController *mapView = [[ViewController alloc] initWithNibName:@"ViewController" bundle:[NSBundle mainBundle]]; [self.navigationController pushViewController:mapView animated:YES];Ostrander
Where you are allocating navigation controller? Just explain how you are adding the view controllers on it.Heehaw
C
4

once go through this,

UISwipeGestureRecognizer  *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleleftSwipe:)];
swipeLeft.numberOfTouchesRequired = 1;//give required num of touches here ..
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.delegate = (id)self;
[self. view addGestureRecognizer:swipeLeft];

UISwipeGestureRecognizer  *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handlerightSwipe:)];
swipeRight.numberOfTouchesRequired = 1;//give required num of touches here ..
swipeRight.delegate = (id)self;
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];

now define swipe methods like below:

 -(void)handleleftSwipe:(UISwipeGestureRecognizer *)recognizer{
//Do ur code for Push/pop..
  }
-(void)handlerightSwipe:(UISwipeGestureRecognizer *)recognizer{
 //Do ur code for Push/pop..
  }

Hope it helps you...

Canna answered 26/6, 2013 at 6:24 Comment(0)
C
1

Try this...

in .h file

@interface MenuViewController : UIViewController {
    ViewController *mapViewObj;
}

in .m file

-(void) viewDidLoad {
    [super viewDidLoad];

    mapViewObj = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

    UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
    [self.view addGestureRecognizer:swipeLeftGesture];
    swipeLeftGesture.direction=UISwipeGestureRecognizerDirectionLeft;
}

-(void)handleSwipeGesture:(UIGestureRecognizer *) sender {
    NSUInteger touches = sender.numberOfTouches;
    if (touches == 2)     {
        if (sender.state == UIGestureRecognizerStateEnded)    {
            //push mapViewObj over here..
            [self.navigationController pushViewController:mapViewObj animated:YES];
        }
    }  
}
Cato answered 26/6, 2013 at 6:28 Comment(3)
Ah, to no avail good sir. I loaded it up into my header and implementation file. I'm not sure if I'm doing this correctly either, but I'm actually using the Google maps API. Does the mapView not go directly into the default ViewController? I appreciate your efforts, though.Ostrander
replace your code at line [self.navigationController pushViewController:mapViewObj animated:YES];Cato
I'm sorry, but I'm not following. What should I replace that section of code with?Ostrander
W
1

This is what I coded for you.

//add gesture recogniser to your view
[self addSwipegestureToView:self.view];


- (void) addSwipegestureToView : (UIView *) view{
    UISwipeGestureRecognizer *_swipegestureRecogniser = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesturePerformed)];
    _swipegestureRecogniser.numberOfTouchesRequired = 2;
    [_swipegestureRecogniser setDirection:UISwipeGestureRecognizerDirectionLeft];
    [view addGestureRecognizer:_swipegestureRecogniser];
}

- (void) swipeGesturePerformed{
    SecondViewController *object = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:object animated:YES];
}

What you only need to have is, currentViewController must have navigationController for appropriate push (slide in) navigation.

Wanda answered 26/6, 2013 at 6:55 Comment(0)
A
0

First of all, you cannot use the built in navigation mechanism.

You will need to add the view of the 'ViewController' to the view of 'MenuViewController' and I recommend you add 'ViewController' as a child view controller to the 'MenuViewController'.

After that, set the frame of 'ViewController' out side of the screen, and when you swipe or do your gesture, just animate it back to the screen.

Arbuckle answered 26/6, 2013 at 6:14 Comment(0)
W
0

This sounds like a perfect time to use UIGestureRecognizer or, more specifically, UISwipeGestureRecognizer

Worthington answered 26/6, 2013 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.