在多个UIViewControllers上重用相同的UIView

时间:2014-06-25 13:48:15

标签: ios objective-c cocoa-touch uiview

背景 - 我有一个有三个UIVIewControllers的应用,就像子应用一样,即它们彼此显示非常不同的内容(但同样是主题)。例如,你可以有一个'动物应用程序',三个UIViewControllers可能是'狗','猫','兔子'。它们的UINavigationBar带有“设置”按钮,可以加载自定义UIView。此UIView动画显示在屏幕上,并显示特定于UIViewController的设置。

问题 - 我希望有一个UIView可以被任何UIViewControllers调用,但它的内容,在这种情况下是一个子菜单,将特定于那个'子app'。

到目前为止我的进展如果代码全部写在调用它的UIView中,我已设法让UIViewController正常工作。创建UIView的正确方法是什么,使用参数调用它(例如放入子菜单中的内容)。

目前我在NavigationItem.rightBarButtonItem上有以下代码:

-(void)showSettings:(id)sender
{
    if (_menuCounter == 0) {
        [self addOverlay];
        _settingsSubView = [[UIView alloc] initWithFrame:CGRectMake((_screenWidth * 0.5), (_barHeight - (_screenHeight * 0.5)), (_screenWidth * 0.5), (_screenHeight * 0.5))];
        _settingsSubView.backgroundColor = [UIColor purpleColor];
        NSLog(@"Settings width  = %fl", _barHeight);
        NSLog(@"Screen width    = %fl", _screenWidth);
        NSLog(@"Settings X      = %fl, Settings height = %fl", _settingsSubView.frame.origin.x, _settingsSubView.frame.size.height);

        UITableView *settingsTSV = [[UITableView alloc] initWithFrame:CGRectMake((_screenWidth * 0.05), (_barHeight), (_screenWidth * 0.4), (_screenHeight * 0.4))];

        UIButton *settingsCloseBtn = [[UIButton alloc] init];
        [settingsCloseBtn setFrame:CGRectMake((_screenWidth * 0.4), (_screenHeight * 0.45), (_screenWidth * 0.1), (_screenHeight * 0.05))];

        [settingsCloseBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [settingsCloseBtn setTitle:@"Close" forState:UIControlStateNormal];
        [settingsCloseBtn addTarget:self action:@selector(closeMenu)forControlEvents:UIControlEventTouchUpInside];
        [settingsCloseBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];

        [_settingsSubView addSubview:settingsTSV];
        [_settingsSubView addSubview:settingsCloseBtn];

        [self.view addSubview:_settingsSubView];
        [self openMenu];
    } else {
        [self closeMenu];
    }
}
-(void)closeMenu
{
    _menuCounter = 0;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.3f];
    [_settingsSubView setFrame:CGRectMake((_screenWidth * 0.5), (_barHeight - (_screenHeight * 0.9)), (_screenWidth * 0.5), (_screenHeight * 0.5))];
    [UIView commitAnimations];
    NSLog(@"%fl",_settingsSubView.frame.origin.y);
    [_overlayView removeFromSuperview];
}
-(void)openMenu
{
    _menuCounter = 1;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.3f];
    [_settingsSubView setFrame:CGRectMake((_screenWidth * 0.5), (_barHeight), (_screenWidth * 0.5), (_screenHeight * 0.5))];
    [UIView commitAnimations];
}
-(void)addOverlay
{
    _overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, _barHeight, _screenWidth, _screenHeight)];
    _overlayView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
    [self.view addSubview:_overlayView];
}

那么,我将如何制作UIView(SubMenuView)可以被任何UIViewControllers调用,并且具有与此处相同的动画,但基于“请求”UIVewController

编辑 - 我已经尝试了更多的东西,似乎无法让这个工作,不确定类别是我现在需要的,因为我似乎被困在'舞台前'使用类别';我甚至无法正确加载UIView。这是我想要实现的目标的图像:

enter image description here

请注意,三个UIViewControllers不同,但包含子菜单的UIViewUIView相同,但在加载之前,会发送不同的参数,因此在绘制时正确的菜单项。

In还尝试导入SubMenuView.h,然后创建它的实例:

SubMenyView *subMenuViewInstance = [SubMenuView alloc] innit];
[self.view addSubView:subMenuViewInstance];

然后加载SubMenuView.m,但它没有引用调用它的UIViewController,因此我无法绘制UIView或编辑UINavigationController。从UIViewController执行此操作通常只需使用

[self.view addSubView:_settingsSubView];
// Run the method that animates the UIView onto the screen...

基本上,我如何使用单独的UIView类来完成最后两行,而不是在每个UIViewControllers的UIViewController.m中编写所有代码?

1 个答案:

答案 0 :(得分:0)

您可以创建一个类别,例如:

#import "UIView+OpticalEffects.h"

@implementation UIView (OpticalEffects)

- (void)configureShadowWithColor:(UIColor *)color {
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:self.bounds];
    //and so on
}

- (void)configureShadowWithBlackColor 
{
    [self configureShadowWithColor:[UIColor blackColor]];
}


- (UIImageView *)imageViewFromView 
{
    CGRect screenRect = UIScreen.mainScreen.bounds;
    UIGraphicsBeginImageContext(screenRect.size);
//and so on
    return capturedImageView;
}

- (void)addInnerVerticalShadow 
{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer] ;
    gradientLayer.frame = CGRectMake(0, 0, 20, self.bounds.size.height);
//and so on
}

@end