如何在XCode中创建一个可折叠的分屏视图



我有一个UINavigationController它包含几个uiviewcontroller。我想创建一个共享的"面板",显示在每个视图的顶部。另外,我想让面板在点击时展开和折叠,覆盖视图。

View1

-Top Panel(可折叠)

小镇面板

View2

-Top Panel(可折叠)

小镇面板

这将类似于工具栏或导航面板隐藏/显示相机控制器的方式,但它将是一个自定义视图。这是一个iPhone应用程序。

作为一名新的XCode开发人员,我希望能从架构的角度了解如何处理这个问题。

创建一个UIViewController子类,比如叫做PanelStyleUIViewController,这是所有有面板的视图的超类。父类实现了面板的控制器逻辑,以及视图扩展和收缩,它们总是发生在子控制器的常规视图逻辑之上。

对于一个新的iOS/cocoa开发人员来说,这将是一个中等难度的项目,因为:

  • 您可能最终不得不以编程方式编写大量折叠面板的视图代码。使用更高级的技术在接口构建器中设计它是可能的,但IB的基本用法是一次设计一个视图控制器。你需要设计一个局部的父视图控制器,然后被继承到许多不同的视图控制器。
  • 你需要确保折叠视图总是在低级视图控制器类的常规视图内容之上(z-index方向)。这可能是通过在面板视图上做一个bringSubviewToFront调用来解决的,比如viewDidAppear方法。
  • 你正在"偏离"标准iPhone应用程序的行为。每当你这样做的时候,你就会让自己头疼,可能会发现自己陷入了死胡同。我的建议是,在你对objective C、cocoa touch等相当有信心之前,先保持"行内"一段时间。

也就是说,这里是我在堆栈溢出编辑器中编写的一些未经测试的代码,应该可以让您了解我对这个超类设计的含义:

// 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];
}

最新更新