2011-04-07 88 views
3

我有一個包含多個UIViewControllers的UINavigationController。我想創建一個顯示在每個視圖頂部的共享「面板」。另外,我想讓該面板在挖掘時展開和摺疊,覆蓋視圖。如何創建一個拆分視圖,其中頂部面板可在XCode中摺疊?

視圖1

機頂面板(可摺疊)

- 主要面板

視圖2

機頂面板(可摺疊)

- 主要面板

這將類似於工具欄或導航欄的方式anel隱藏/顯示相機控制器,但這將是一個自定義視圖。這是一個iPhone應用程序。

作爲一名新的XCode開發人員,我希望能夠從架構的角度來理解這個問題。

+0

你可以添加一個草圖嗎? – 2011-04-07 16:45:19

回答

1

創建一個UIViewController子類,稱爲PanelStyleUIViewController, 這將是所有將具有該面板的視圖的超類。超類實現了面板的控制器邏輯,以及視圖擴展和收縮,它總是發生在子控制器的常規視圖邏輯之上。

這將是一個新的iOS /可可開發一個適度困難的項目,這是因爲:

  • 你可能最終不得不以編程寫了很多摺疊面板的視圖代碼。可以使用更先進的技術在接口構建器中進行設計,但IB的基本用法是一次設計一個視圖控制器。您需要設計一個部分,父視圖控制器,然後繼承到許多不同的控制器。
  • 您需要確保摺疊視圖始終位於較低級別視圖控制器類的常規視圖內容之上(z-index wise)。這可能是通過在viewDidAppear方法的面板視圖上執行bringSubviewToFront調用來解決的。
  • 從「標準iPhone應用程序」的行爲方式來看,你正在「越野」。每當你這樣做的時候,你會爲自己頭痛,並且可能會發現自己陷入死衚衕。我的建議是留一會兒「行內」,直到你用客觀的C,可可觸摸等

這就是說非常有信心,這裏的一些未經測試的代碼,我寫在這裏的堆棧溢出的編輯器,應該給你一個關於這個超類設計意味着什麼的概念:

// PanelStyleUIViewController.h 
@interface PanelStyleUIViewController : UIViewController { 
    UIView *panelView; 
} 
@property (nonatomic, retain) UIView *panelView; 

// PanelStyleUIViewController.m 

@implementation PanelStyleUIViewController 

@synthesize panelView; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // setup geometry, contents, etc of panelView programmatically... 
    self.panelView = [[[UIView alloc] init] autorelease]; 
    panelView.frame = CGRectMake(0,0,320,200); 
    // set resizing mask appropriately for landscape support 
    panelView.autoresizingMask = UIViewAutoresizingMaskFlexibleWidth | UIViewAutoresizingMaskFlexibleBottomMargin; 
    // put a button on the panel view so it can be tapped to slide down 
    UIButton *slidePanelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    slidePanelButton.frame = CGRectMake(0,160,320,40); 
    [slidePanelButton addTarget:self action:@selector(slidePanel) forControlEvents:UIControlEventTouchUpInside]; 
    [panelView addSubview:slidePanelButton]; 
    // set panelView with a transform offset to slide it up under the top of the screen 
    panelView.transform = CGAffineTransformMakeTranslation(0, -160); 
    // add panelView to regular view controller's view 
    [self.view addSubview:panelView]; 
} 

- (void)viewWillAppear { 
    // make sure the panel shows up on top, z-index wise, since subclasses might have created views that overlap it. 
    [self.view bringSubviewToFront:panelView]; 
} 

- (void)slidePanel { 
    // remove the panel transform in an animated fashion (slide down). 
    // TODO: some button or action needs to slide the panel back up... 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.3]; 
    [panelView setTransform:CGAffineTransformMakeTranslation(0,0)]; 
    [UIView commitAnimations]; 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // always make sure you clean up progammatically-retained views here 
    self.panelView = nil; 
} 

- (void)dealloc { 
    // and here too 
    self.panelView = nil; 
    [super dealloc]; 
}