2014-06-25 34 views
1

背景 -我有三個UIVIewControllers行爲像子應用,即它們顯示出彼此非常不同的內容(但同樣主題)的應用程序。例如,你可以有一個'動物應用程序',三個UIViewControllers可以是'狗','貓','兔'。這些有一個UINavigationBar與'設置'按鈕加載自定義UIView。這UIView動畫到屏幕上並顯示特定於該設置UIViewController的設置。重用多個UIViewControllers相同的UIView

問題 -我想有一個UIView,可以由任何UIViewControllers的被調用,但它的內容,在這種情況下的子菜單,將具體到「子應用」。

我迄今取得的進展 -我設法得到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是不同的,但包含的子菜單是一樣的UIView但被加載在它之前,不同的參數發送UIView所以當它繪製它有正確的菜單項。

在還試圖導入SubMenuView.h,然後創建它的一個實例:那麼

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

SubMenuView.m加載但它沒有參考UIViewController調用它,所以我可以」 t畫出UIView或編輯UINavigationController。從UIViewController這樣做,我通常只使用

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

所以基本上,我該怎麼辦最後兩行有獨立的UIView類,而不是每個UIViewControllers的UIViewController.m內,以書面所有的代碼?

回答

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 
+0

我怎麼會叫這個從UIViewControllers之一嗎? – Ryan

+0

您可以在創建UIView的任何時候使用該類別。只需要導入.h文件。這些方法可以訪問。 (不要忘了在.h文件中公開它們。 – iOSNoob

+0

這裏是一個鏈接到一個教程:http://rypress.com/tutorials/objective-c/categories.html – iOSNoob

相關問題