2012-10-16 21 views
1

建立一個滑出式菜單我新的Xcode開發,我想發展我的iPhone應用程序從一個UIToolbar引發了slide-up menu如何在Xcode

我在看的是創建一個帶菜單按鈕的子視圖,將其添加到主視圖,並在點擊toggle按鈕時滑動(可見)或向下(隱藏)。

如何以編程方式執行此操作?適用於iPhone的Opera應用程序做得很好(請參閱圖片)。

opera-app

回答

5

你所說的可以通過代碼添加一個子視圖到您MAINVIEW和滑動上下以使其可見和unvisible來完成。

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文件中,然後在將子視圖添加到mainview之後添加這些行,導入爲#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; 
} 

} 

可能有很多簡單的方法,但希望這會讓你開始:)

+0

嗨Naveen,非常感謝!這有助於完成我的上半任務! 我現在想在菜單視圖中實現類似網格的佈局,就像在我的附件中一樣。 我可以使用帶有UICollectionViewCells的UICollectionView來做到這一點嗎?我嘗試過,但我在初始化視圖和單元格時遇到了問題。 –

+0

@VinodVishwanath簡單的方法是以這種方式添加自定義按鈕並給它們背景圖像 – superGokuN

+0

以編程方式將按鈕添加到子視圖。 – superGokuN