使用轻扫手势切换视图控制器

好吧,这是我遇到的问题:

我试图从一个viewController ,我命名MenuViewController其中包含我的菜单(显然)切换。 我有一个名为ViewController的单独的viewController ,它包含我的mapView 。 我希望能够从我的MenuViewController swipe left指滑过我的mapView

我不确定从哪里开始。

另外,我正在使用xib文件,而不是故事板。 运行iOS 6

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

一旦经过这个,

 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]; 

现在定义如下的滑动方法:

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

希望它可以帮助你…

尝试这个…

在.h文件中

 @interface MenuViewController : UIViewController { ViewController *mapViewObj; } 

在.m文件中

 -(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]; } } } 

这是我为你编码的。

 //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]; } 

你只需要有currentViewController必须有navigationController适当的推(滑入)导航。

首先,你不能使用内置的导航机制。

你需要将'ViewController'的视图添加到'MenuViewController'视图中,我build议你将'ViewController'作为子视图控制器添加到'MenuViewController'中。

之后,将“ViewController”的框架设置在屏幕的外侧,当您滑动或做手势时,只需将其设置回屏幕即可。

这听起来像是使用UIGestureRecognizer的最佳时机,更具体地说,是使用UISwipeGestureRecognizer