1

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

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

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

歌剧应用

4

1 回答 1

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

}

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

于 2012-10-16T14:16:48.937 回答