如何使用uiviewcontroller实现滑出菜单。

时间:2016-06-14 09:21:09

标签: ios objective-c uiviewcontroller

  

大家好我正在我的应用程序中滑出菜单。我在mainviewcontroller上使用以下内容呈现一个viewcontroller。

- (IBAction)MenuButton:(id)sender {


 tlc = [self.storyboard instantiateViewControllerWithIdentifier:@"SlideOut"];
[tlc.view setFrame:CGRectMake(-800, 0, self.view.frame.size.width, self.view.frame.size.height)];

[self addChildViewController:tlc];
[self.view addSubview:tlc.view];
[tlc didMoveToParentViewController:self];

[UIView animateWithDuration:0.3 animations:^{
    [tlc.view setFrame:CGRectMake(-800, 0, self.view.frame.size.width, self.view.frame.size.height)];

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

}
  

我需要使用UIGesturerecogniton打开和隐藏视图,所以我使用以下代码,通过此代码我能够呈现视图但我无法使用手势识别隐藏它。请看我的代码。

-(void)SwipGestureAction {
NSLog(@"gesture");
UISwipeGestureRecognizer *swiperight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(SwipGestureRightAction:)];
swiperight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperight];
   }


-(void)SwipGestureRightAction:(UISwipeGestureRecognizer *)swipeRight {

NSLog(@"left");

tlc = [self.storyboard instantiateViewControllerWithIdentifier:@"SlideOut"];
[tlc.view setFrame:CGRectMake(-800, 0, self.view.frame.size.width, self.view.frame.size.height)];

[self addChildViewController:tlc];
[self.view addSubview:tlc.view];
[tlc didMoveToParentViewController:self];


                     [UIView animateWithDuration:0.3 animations:^{
                         [tlc.view setFrame:CGRectMake(-800, 0, self.view.frame.size.width, self.view.frame.size.height)];

                         swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(SwipGestureLeftAction:)];
                         swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
                         [self.view addGestureRecognizer:swipeLeft];

                     }];



 }


  -(void)SwipGestureLeftAction:(UISwipeGestureRecognizer *)swipeRight {

                     NSLog(@"right");



                     [UIView animateWithDuration:0.3 animations:^{
                         [tlc.view setFrame:CGRectMake(-800, 0, self.view.frame.size.width, self.view.frame.size.height)];
                         [self.view removeGestureRecognizer:swipeLeft];
                         }];




}

1 个答案:

答案 0 :(得分:1)

在几天前我有同样的问题兄弟;

我已使用以下教程解决了该问题。

这对我来说是完美的答案......我希望这也对你有所帮助.. :)

https://www.raywenderlich.com/32054/how-to-create-a-slide-out-navigation-like-facebook-and-path

https://github.com/John-Lluch/SWRevealViewController