以编程方式滑动菜单

时间:2014-10-30 15:04:40

标签: ios objective-c xcode

我有一个名为“home”的课程和一个名为“menu”的课程,在家里我创建了一个按钮来推送菜单,当我点击按钮时,我让框架移动并显示菜单但是我可以点击菜单的表格视图单元格,优先级为主页。我怎样才能改变这项工作?

- (void)viewDidLoad {

   [super viewDidLoad];
    menuViewController = [[ITMenuViewController alloc] init];
    menuViewController.view.frame = CGRectMake(0, 0, 0, 568);
    [self.view addSubview:menuViewController.view];

    btn = [[UIButton alloc] init];

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn addTarget:self action:@selector(pushMenu) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"push" forState:UIControlStateNormal];
    btn.frame = CGRectMake(40, 300, 240, 40);
    [btn setBackgroundColor:[UIColor orangeColor]];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.layer.cornerRadius = 2;
    btn.tag = 1;
    btn.clipsToBounds = YES;
    //set other button properties here.
    [self.view addSubview:btn];
}

- (void)pushMenu {

    if (menuViewController.view.frame.origin.x < 0 && btn.tag == 2) {
        [UIView animateKeyframesWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{ 

        self.view.frame = CGRectMake(0, 0, 320, 568);
        menuViewController.view.frame=CGRectMake(0, 0, 0, 568);

        } completion:^(BOOL finished) {
             btn.tag = 1;
        }];
     }

     if (menuViewController.view.frame.origin.x >= 0 && btn.tag == 1)
     {
        [UIView animateKeyframesWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{

            self.view.frame = CGRectMake(200, 0, 320, 568);
            menuViewController.view.frame=CGRectMake(-self.view.frame.size.width + 0, 0, self.view.frame.size.width, 568);
            [self.view.layer setCornerRadius:4];
            [self.view.layer setShadowColor:[UIColor blackColor].CGColor];
            [self.view.layer setShadowOpacity:0.8];
            [self.view.layer setShadowOffset:CGSizeMake(0.0f, 2.5f)];        
        } completion:^(BOOL finished) {
            btn.tag = 2;
        }];
    }}
}

1 个答案:

答案 0 :(得分:0)

检查PPRevealSideViewController。它可以帮助您实现所需的目标https://github.com/ipup/PPRevealSideViewController

您可以在appdelegate中添加家庭视图控制器,如下所示:

HomeViewController *home = [[HomeViewController alloc initWithNibName:@"MainViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:home];
_revealSideViewController = [[PPRevealSideViewController alloc] initWithRootViewController:nav];

self.window.rootViewController = _revealSideViewController;

按这样推动你的manu viewcontroller:

MenuViewController *menu = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil ];
[self.revealSideViewController pushViewController:menu onDirection:PPRevealSideDirectionBottom animated:YES];

修改

要将PPRevealSideViewController作为子模块添加到项目中,请转到终端:

$ cd /path/to/MyApplication
# If this is a new project, initialize git...
$ git init
$ git submodule add git://github.com/ipup/PPRevealSideViewController.git vendor/PPRevealSideViewController
$ git submodule update --init --recursive

- 在你的XCode项目中,从PPRevealSideViewController文件夹中取出PPRevealSideViewController.h和.m并将它们拖到你的项目中。

- 将PPRevealSideViewController.h文件导入PCH文件或AppDelegate文件。

- 添加QuartzCore Framework。

- 开始使用这个新控制器!