在我的主視圖中,我做了一些手勢操作,導致顯示一些新的視圖。此時我想將整個背景(除了這個新視圖)調暗爲良好的UI練習。在HTML中,Javascript看起來像這樣 - 我如何在iOS中獲得同樣的效果?iOS在顯示視圖時暗淡的背景
回答
萊在背景一個UIView,設置它的背景[UIColor blackColor]
,並設置其不透明度爲喜歡0.6。然後將該UIView添加到父視圖。
這將模糊背景並攔截任何點擊背景控件。
雖然Dan的建議是針對目標的,但在這種情況下,您不能使用模式視圖控制器,因爲即使您的視圖背景爲透明,典型模式仍會覆蓋整個屏幕,從而阻止父視圖控制器將會看到你在應用程序的UIWindow中擁有的任何東西)。
如果你在iPad上這樣做,有兩種模式演示風格(UIViewModalPresentationStylePageSheet
和UIViewModalPresentationStyleFormSheet
)可以做類似的事情,但這些風格不適用於iPhone或iPod Touch。
將「陰影」視圖與深色背景和部分不透明度以及您希望處於前景的任何視圖添加到視圖控制器的視圖中。您可以使用標準UIView動畫塊或CoreAnimation爲它們製作動畫。另一個需要注意的是,如果你想截取觸摸到這個陰影視圖,你可以使它成爲一個巨大的按鈕,UIView的子類覆蓋觸摸方法中的一個,比如touchesEnded:
,或者將其更改爲UIControl,它可以接收像觸摸事件一樣的UIButton,但沒有額外的文本,陰影,狀態等選項
你說得對,我在回答中確實說了「莫代爾」。我並不是指任何形式的「模態呈現」的觀點。基本上,你正在構建自己的模態視圖。 –
上述兩個答案的工作,如果新視圖有它自己的'背景',即沒有透明度。這個問題導致我在這裏無法解決,但我試圖做的是:我在屏幕上運行一個AVCaptureSession
,通常AVCaptureVideoPreviewLayer
實時顯示視頻。我想選擇屏幕的一部分(以後只做這部分操作),並選擇此部分時,要將其餘的視頻預覽調暗。這是什麼樣子:
這是我如何解決了這個:一個新的視圖中創建不同的地方觸摸屏幕,並加入視頻預覽視圖的子視圖。然後,我用前面提到的背景顏色和不透明度創建4個額外的,不重疊的矩形子視圖,以覆蓋屏幕的其餘部分。我明確需要將所有這些視圖而不是作爲主視圖的子視圖,因爲我需要能夠在其他位置觸摸屏幕並更改選定區域。
我敢肯定,有更多優雅的方法來解決這個問題,所以如果有人知道如何評論...
#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的圖形界面優化的代碼,但我不得不編程寫代碼,能夠測試它
這樣的想法是:
- 創建UIView名稱它是你想要的(dimBackgroundUIView)比設置 背景色爲黑色並將阿爾法設置爲0.1或0.2或....
- 當你需要調暗背景時,添加這2行代碼:[self.view addSubview:dimBackgroundUIView]; [self addConstraintsForDimView];
- 並添加這2行代碼,當你想刪除調暗 背景:if(dimBackgroundUIView)[dimBackgroundUIView removeFromSuperview];
- 1. 視頻播放時暗淡的背景
- 2. 背景暗淡不正常
- 3. 使背景視圖變暗,就像UIAlertView顯示時一樣
- 4. 如何在顯示自定義視圖時調暗背景視圖
- 5. 添加的UIView在暗淡的背景
- 6. iOS - 旋轉視圖顯示背景
- 7. 暗淡進度條的背景
- 8. 在懸停時淡化背景圖像以顯示背景顏色?
- 9. 活動中暗淡或模糊背景
- 10. 暗淡/模糊父佈局背景
- 11. 在iOS上不顯示背景圖像
- 12. 在顯示活動指示符時使背景變暗?
- 13. 同時淡入淡出背景圖片
- 14. 圍繞視圖調暗背景
- 15. 在視頻背景自動播放後顯示圖像背景
- 16. 在背景圖像中淡入淡出
- 17. 在背景圖片中淡入淡出
- 18. 加載uiwebview時的背景圖像ios有時會顯示
- 19. 變暗CSS背景圖像
- 20. 變暗CSS背景圖片?
- 21. 在其他視圖的背景中顯示視圖?
- 22. 顯示背景圖片在
- 23. IOS - 暗淡父視圖時添加一個顏色選擇器子視圖
- 24. 如何在asp.net中顯示面板時淡化背景
- 25. 淡入淡出中的背景圖像
- 26. 在jQuery中彈出對話框後暗淡的背景
- 27. 子視圖在添加時顯示爲灰色背景色
- 28. 在背景視頻加載時顯示圖像
- 29. 在黑暗的背景下不顯示Android進度條
- 30. 淡入/淡出背景圖像沒有白色背景
你可以在界面生成器中做這個佈局,然後只是以編程方式隱藏,直到你想要它?或者即使在隱藏時它也會以某種方式干擾? – ToolmakerSteve