ios - 如何使用uiviewcontroller实现滑出式菜单?

标签 ios objective-c uiviewcontroller

Hi all i am working on slide out menu in my app. i present a viewcontroller using the following on mainviewcontroller.

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

}

i need to open and hide view using UIGesturerecogniton, so i use the following code, by this code i am able to present view but i am not able to hide it using gesture recognition. please look at my code.

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




}

最佳答案

前几天我也遇到过同样的问题,兄弟;

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

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

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

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

关于ios - 如何使用uiviewcontroller实现滑出式菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37807886/

相关文章:

iphone - 核心数据——如何插入数组

ios - 通过Instagram应用而非Safari登录

iphone - 收到通知的频率比发送通知的频率高

xcode - 使用 Storyboard或以编程方式创建 View 时自定义初始化

ios - ViewControllers 作为实例变量

ios - 使用 UINavigationController 管理 UIViewController 层次结构

ios - 添加 UIImage 会忽略 UIImageView 框架并调整其大小

ios - 如何获取导航后退按钮标题?

ios - 调用 stopDeviceMotionUpdates 时 CoreMotion 崩溃(仅限 iPad)

ios - 相机胶卷中的文件发生更改时如何刷新 UIImagePickerController?