如何在Xcode中构建一个Slide-Up菜单

时间:2012-10-16 14:05:38

标签: iphone xcode ios5 user-interface

我是Xcode开发的新手,我想在我的iPhone应用中开发slide-up menu UIToolbar

我正在看的是创建带有菜单按钮的子视图,将其添加到主视图,并在点击toggle按钮时向上滑动(可见)或向下(隐藏)。

我如何以编程方式执行此操作?!适用于iPhone的 Opera应用程序做得很好(见图)。

opera-app

1 个答案:

答案 0 :(得分:5)

您所说的内容可以通过在主视图中添加子视图并上下滑动以使其可见且不可见来完成。

1)将子视图添加到远y坐标,以便最初在视图中不可见。

subView  =  [[UIView alloc]initWithFrame:CGRectMake(0,470,320,200)]]; // subView is an ivar
// Add stuffs to your subview
[self.view addSubview:subView];

2)现在制作两个IBActions showMySubview和hideMySubview并将它们链接到相应的按钮,或者你可以通过检查它的label.text来做一些切换。

3)在你的showMySubview

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.5];
        [UIView setAnimationDelay:0.0];
        subView.frame = CGRectMake(0, 50, 320, 200);
        [UIView commitAnimations];

4)在hideMySubview

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.5];
        [UIView setAnimationDelay:0.0];
        editPopUpView.frame = CGRectMake(0, 470, 320, 200);
        [UIView commitAnimations];

您还可以对子视图进行一些美化,以便为您的项目添加 QuartzCore 框架,并将其导入.m文件,并在将子视图添加到主视图后添加这些行import是 #import“QuartzCore / QuartzCore.h”

[[subView layer] setCornerRadius:12.0f];
[[subView layer] setMasksToBounds:YES];
[[subView layer] setBorderWidth:4.0f];
[subView layer].borderColor =   [UIColor purpleColor].CGColor;

希望无论如何这将对你有帮助:))

编辑:

通过代码向您的子视图添加按钮:

for (int i = 0; i < 3; i++) {

    for (int j = 0; j < 3; j++) {

        UIButton *btn   =   [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame       =   CGRectMake(j*60,200+ 35 * i ,50 , 30);
        //[btn setTitle:@"Test" forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateHighlighted];
        btn.tag =   (j + 1) + (3 * i);
        [btn addTarget:self action:@selector(subViewButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        btn.showsTouchWhenHighlighted   =   YES;
        [subView addSubview:btn]; // its the subview we added to our main view add this after initializing the subview

    }
}

现在可以捕捉所有按钮的点击次数

-(IBAction)subViewButtonClicked:(id)sender
{
    switch ([sender tag]) {
    case 1:
    {
        // Do your stuff here

         NSLog(@"the tag of sender is %i",[sender tag]);

        break;
    }

    case 2:
    {
        // Do your stuff here

         NSLog(@"the tag of sender is %i",[sender tag]);

        break;
    }
    case 3:
    {
        // Do your stuff here

         NSLog(@"the tag of sender is %i",[sender tag]);

        break;
    }
    case 4:
    {
        // Do your stuff here

         NSLog(@"the tag of sender is %i",[sender tag]);

        break;
    }
    case 5:
    {
        // Do your stuff here

         NSLog(@"the tag of sender is %i",[sender tag]);

        break;
    }

    case 6:
    {
        // Do your stuff here

         NSLog(@"the tag of sender is %i",[sender tag]);

        break;
    }
    case 7:
    {
        // Do your stuff here

         NSLog(@"the tag of sender is %i",[sender tag]);

        break;
    }
    case 8:
    {
        // Do your stuff here

         NSLog(@"the tag of sender is %i",[sender tag]);

        break;
    }

    case 9:
    {
        // Do your stuff here

         NSLog(@"the tag of sender is %i",[sender tag]);

        break;
    }
    default:
        break;
}

}

可能有很多简单的方法,但希望这会让你有个开始:)