2011-07-05 52 views
20

在我的主視圖中,我做了一些手勢操作,導致顯示一些新的視圖。此時我想將整個背景(除了這個新視圖)調暗爲良好的UI練習。在HTML中,Javascript看起來像這樣 - 我如何在iOS中獲得同樣的效果?iOS在顯示視圖時暗淡的背景

enter image description here

回答

33

萊在背景一個UIView,設置它的背景[UIColor blackColor],並設置其不透明度爲喜歡0.6。然後將該UIView添加到父視圖。

這將模糊背景並攔截任何點擊背景控件。

+0

你可以在界面生成器中做這個佈局,然後只是以編程方式隱藏,直到你想要它?或者即使在隱藏時它也會以某種方式干擾? – ToolmakerSteve

5

雖然Dan的建議是針對目標的,但在這種情況下,您不能使用模式視圖控制器,因爲即使您的視圖背景爲透明,典型模式仍會覆蓋整個屏幕,從而阻止父視圖控制器將會看到你在應用程序的UIWindow中擁有的任何東西)。

如果你在iPad上這樣做,有兩種模式演示風格(UIViewModalPresentationStylePageSheetUIViewModalPresentationStyleFormSheet)可以做類似的事情,但這些風格不適用於iPhone或iPod Touch。

將「陰影」視圖與深色背景和部分不透明度以及您希望處於前景的任何視圖添加到視圖控制器的視圖中。您可以使用標準UIView動畫塊或CoreAnimation爲它們製作動畫。另一個需要注意的是,如果你想截取觸摸到這個陰影視圖,你可以使它成爲一個巨大的按鈕,UIView的子類覆蓋觸摸方法中的一個,比如touchesEnded:,或者將其更改爲UIControl,它可以接收像觸摸事件一樣的UIButton,但沒有額外的文本,陰影,狀態等選項

+3

你說得對,我在回答中確實說了「莫代爾」。我並不是指任何形式的「模態呈現」的觀點。基本上,你正在構建自己的模態視圖。 –

1

上述兩個答案的工作,如果新視圖有它自己的'背景',即沒有透明度。這個問題導致我在這裏無法解決,但我試圖做的是:我在屏幕上運行一個AVCaptureSession,通常AVCaptureVideoPreviewLayer實時顯示視頻。我想選擇屏幕的一部分(以後只做這部分操作),並選擇此部分時,要將其餘的視頻預覽調暗。這是什麼樣子:

這是我如何解決了這個:一個新的視圖中創建不同的地方觸摸屏幕,並加入視頻預覽視圖的子視圖。然後,我用前面提到的背景顏色和不透明度創建4個額外的,不重疊的矩形子視圖,以覆蓋屏幕的其餘部分。我明確需要將所有這些視圖而不是作爲主視圖的子視圖,因爲我需要能夠在其他位置觸摸屏幕並更改選定區域。

我敢肯定,有更多優雅的方法來解決這個問題,所以如果有人知道如何評論...

1
#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController<UIGestureRecognizerDelegate> 
{ 
    UIView *mainView; 
    UIView *dimBackgroundUIView; 
    UIView *customView; 

    UIButton *btn; 
} 

@end 


#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    mainView = [[UIView alloc] init]; 
    mainView.backgroundColor = [UIColor whiteColor]; 
    [self.view addSubview:mainView]; 
    [self addConstraintsForMainView]; 

    btn = [[UIButton alloc] initWithFrame:CGRectMake(200, 200, 100, 30)]; 
    [btn setTitle:@"Button" forState:UIControlStateNormal]; 
    [btn setBackgroundColor:[UIColor blueColor]]; 
    [btn addTarget:self action:@selector(showCustomView_Action:) forControlEvents:UIControlEventTouchUpInside]; 
    [mainView addSubview:btn]; 

    dimBackgroundUIView = [[UIView alloc] init]; 
    dimBackgroundUIView.backgroundColor = [UIColor blackColor]; 
    dimBackgroundUIView.alpha = 0.2; 
} 

-(void)removeCustomViewsFromSuperView { 

    if(dimBackgroundUIView) 
     [dimBackgroundUIView removeFromSuperview]; 

    if(customView) 
     [customView removeFromSuperview]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)showCustomView_Action:(id)sender { 

    customView = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 80, 80)]; 
    customView.backgroundColor = [UIColor redColor]; 

    [self.view addSubview:dimBackgroundUIView]; 
    [self addConstraintsForDimView]; 

    [self.view addSubview:customView]; 

    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeCustomViewsFromSuperView)]; 
    tapped.delegate=self; 
    tapped.numberOfTapsRequired = 1; 
    [customView addGestureRecognizer:tapped]; 
} 

-(void) addConstraintsForDimView { 

    [dimBackgroundUIView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:dimBackgroundUIView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:0]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:dimBackgroundUIView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:0]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:dimBackgroundUIView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:dimBackgroundUIView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]; 
} 

-(void) addConstraintsForMainView { 

    [mainView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:mainView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:0]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:mainView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:0]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:mainView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:mainView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]; 
} 

@end 

注意:您可以通過使用Xcode的圖形界面優化的代碼,但我不得不編程寫代碼,能夠測試它

這樣的想法是:

  1. 創建UIView名稱它是你想要的(dimBackgroundUIView)比設置 背景色爲黑色並將阿爾法設置爲0.1或0.2或....
  2. 當你需要調暗背景時,添加這2行代碼:[self.view addSubview:dimBackgroundUIView]; [self addConstraintsForDimView];
  3. 並添加這2行代碼,當你想刪除調暗 背景:if(dimBackgroundUIView)[dimBackgroundUIView removeFromSuperview];