使用滑动手势切换视图控制器

时间:2013-06-26 06:09:17

标签: ios objective-c viewcontroller uiswipegesturerecognizer

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

我正在尝试从一个名为viewController的{​​{1}}切换,其中包含我的菜单(显然)。我有一个名为MenuViewController的单独viewController,其中包含我的ViewController。我希望能够将mapView的{​​{1}}双手指向我的swipe left

我不确定从哪里开始。

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

6 个答案:

答案 0 :(得分:13)

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

答案 1 :(得分:4)

一旦经历过这个,

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

希望它对你有所帮助......

答案 2 :(得分:1)

试试这个......

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

答案 3 :(得分:1)

这是我为你编码的。

//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进行适当的推送(滑入)导航。

答案 4 :(得分:0)

首先,您无法使用内置的导航机制。

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

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

答案 5 :(得分:0)

这听起来像是使用UIGestureRecognizer或更具体地UISwipeGestureRecognizer

的完美时间