如何在 Xcode 中构建上滑菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12916484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:50:27  来源:igfitidea点击:

How to build a Slide-Up Menu in Xcode

iphonexcodeios5user-interface

提问by Vinod Vishwanath

I'm new to Xcode development, and I'd like to develop a slide-up menutriggered from a UIToolbarin my iPhone app.

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

What I'm looking at is creating a subview with menu buttons, adding it to the main view, and sliding it up(visible) or down(hidden) when the togglebutton is tapped.

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

How do I do this programmatically?! The Opera appfor iPhone does this well (see the picture).

我如何以编程方式执行此操作?!该歌剧的应用程序为iPhone做这口井(见图片)。

opera-app

歌剧应用程序

回答by superGokuN

What you are saying can be done by code by adding a subview to your mainview and slide it up and down to make it visible and unvisible.

您所说的可以通过代码来完成,方法是在您的主视图中添加一个子视图并上下滑动使其可见和不可见。

1) Add a subview to far y coordinate so that initially its not visible in the view.

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) Now make two IBActions showMySubview and hideMySubview and link them to the corresponding buttons or you can do some toggling with one button by checking its label.text.

2) 现在创建两个 IBActions showMySubview 和 hideMySubview 并将它们链接到相应的按钮,或者您可以通过检查其 label.text 对一个按钮进行一些切换。

3) In your showMySubview

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) In your hideMySubview

4) 在你的 hideMySubview

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

You can also do some beautify things to your subview to look some good add QuartzCoreframework to your project and import it in your .m file and add these lines after adding your subview to your mainview the import is #import "QuartzCore/QuartzCore.h"

你也可以对你的子视图做一些美化的事情来看起来很好将QuartzCore框架添加到你的项目中并将它导入到你的 .m 文件中,并在将你的子视图添加到主视图后添加这些行,导入是 #import "QuartzCore/QuartzCore.h ”

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

Hope This will help you in anyways :)

希望这无论如何都会对你有所帮助:)

EDIT :

编辑 :

Adding buttons via code to your subview :

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

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

    }
}

Now a function to catch the clicks of all the buttons

现在有一个功能来捕捉所有按钮的点击

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

}

There may be many easy ways out there but hope this will get you a start :)

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