switching view controllers using swipe gestures

2020-03-02 04:35发布

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.

6条回答
倾城 Initia
2楼-- · 2020-03-02 04:41

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...

查看更多
混吃等死
3楼-- · 2020-03-02 04:47

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];
        }
    }  
}
查看更多
走好不送
4楼-- · 2020-03-02 04:48

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.

查看更多
欢心
5楼-- · 2020-03-02 04:48

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

查看更多
Ridiculous、
6楼-- · 2020-03-02 04:57
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    
        }
    }  
}
查看更多
贼婆χ
7楼-- · 2020-03-02 04:59

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.

查看更多
登录 后发表回答