switching view controllers using swipe gestures

混江龙づ霸主 提交于 2019-12-30 06:22:40

问题


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.


回答1:


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



回答2:


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:


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:


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:


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.




回答6:


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



来源:https://stackoverflow.com/questions/17312700/switching-view-controllers-using-swipe-gestures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!