Objective c 当显示视图时,iOS变暗背景
在我的主视图中,我做了一些手势动作,以显示一些新视图。此时,我想将整个背景调暗(除了这个新视图),作为一个良好的UI实践。在HTML、Javascript中看起来是这样的-我如何在iOS中获得同样的效果Objective c 当显示视图时,iOS变暗背景,objective-c,ios,animation,uiview,Objective C,Ios,Animation,Uiview,在我的主视图中,我做了一些手势动作,以显示一些新视图。此时,我想将整个背景调暗(除了这个新视图),作为一个良好的UI实践。在HTML、Javascript中看起来是这样的-我如何在iOS中获得同样的效果 在背景上放置一个UIView,将其背景设置为[UIColor blackColor],并将其不透明度设置为0.6。然后将该UIView添加到父视图 这将使背景变暗并截获对背景控件的任何点击。虽然Dan的建议是正确的,但在这种情况下,您不能使用模态视图控制器,因为典型模态覆盖整个屏幕,阻止父视图控
在背景上放置一个UIView,将其背景设置为
[UIColor blackColor]
,并将其不透明度设置为0.6。然后将该UIView添加到父视图
这将使背景变暗并截获对背景控件的任何点击。虽然Dan的建议是正确的,但在这种情况下,您不能使用模态视图控制器,因为典型模态覆盖整个屏幕,阻止父视图控制器,即使您的视图背景是透明的(您将在应用程序的UI窗口中看到所有内容) 如果您在iPad上执行此操作,则有两种模式演示样式(
UIViewModalPresentationStylePageSheet
和UIViewModalPresentationStyleFormSheet
)可以执行类似的操作,但它们在iPhone或iPod Touch上不起作用
将“阴影”视图直接添加到视图控制器的视图中,该视图具有黑色背景和部分不透明度,以及您希望位于前景的任何视图。您可以使用标准UIView动画块或CoreAnimation为其设置动画
另一个注意事项是,如果您想截取对该阴影视图的触摸,您可以将其设置为巨型按钮,将UIView子类化以覆盖触摸方法之一,如
touchesend:
,或者将其更改为UIControl,该UIControl可以接收触摸事件,如UIButton,但不包括文本、阴影、状态等的额外选项。上述two如果新视图有自己的“背景”,即没有透明度,则回答有效。但是,导致我出现在这里的问题无法通过这种方式解决,我试图做的是:我在屏幕上运行AVCaptureSession
,使用通常的AVCaptureVideoPreviewLayer
实时显示视频。我想选择f屏幕(稍后仅使用此部分执行操作),选择此部分时,希望将视频预览的其余部分调暗。这是它的外观:
这就是我解决这个问题的方法:根据触摸屏幕的位置创建一个新视图,并将其添加为视频预览视图的子视图。然后,我创建4个附加的、不重叠的矩形子视图,使用前面提到的背景色和不透明度覆盖屏幕的其余部分。我明确要求所有这些视图都不是子视图因为我需要在其他地方触摸屏幕并更改所选区域的能力
我相信有更优雅的方法来解决这个问题,所以如果有人知道如何解决,请发表评论…#import
#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
@界面ViewController:UIViewController
{
UIView*主视图;
UIView*dimBackgroundUIView;
UIView*自定义视图;
UIButton*btn;
}
@结束
#导入“ViewController.h”
@界面视图控制器()
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
mainView=[[UIView alloc]init];
mainView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:mainView];
[自添加约束表单查看];
btn=[[UIButton alloc]initWithFrame:CGRectMake(20020010030)];
[btn设置标题:@“按钮”用于状态:uicontrol状态正常];
[btn setBackgroundColor:[UIColor blueColor]];
[btn addTarget:self action:@selector(showCustomView_action:)for ControlEvents:UIControlEventTouchUpInside];
[主视图添加子视图:btn];
dimBackgroundUIView=[[UIView alloc]init];
dimBackgroundUIView.backgroundColor=[UIColor blackColor];
dimBackgroundUIView.alpha=0.2;
}
-(无效)从SuperView中删除自定义视图{
if(dimBackgroundUIView)
[dimBackgroundUIView从SuperView移除];
如果(自定义视图)
[自定义视图从SuperView移除];
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(iAction)showCustomView\u操作:(id)发件人{
customView=[[UIView alloc]initWithFrame:CGRectMake(1002008080)];
customView.backgroundColor=[UIColor redColor];
[self.view addSubview:dimBackgroundUIView];
[自添加约束ForDimView];
[self.view addSubview:customView];
UITapGestureRecognizer*点击=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeCustomViewsFromSuperView)];
代表=自我;
tapped.numberOfTapsRequired=1;
[自定义视图AddGestureRecognitor:点击];
}
-(void)addConstraintsForDimView{
[dimBackgroundUIView SetTranslates自动调整大小GMaskintoConstraints:否];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:dimBackgroundUIView属性:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual-toItem:self.view属性:NSLayoutAttributeLeft乘数:1常量:0];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:dimBackgroundUIView属性:nsLayoutAttributeRights relatedBy:NSLayoutRelationEqual toItem:self.view属性:nsLayoutAttributeRights乘数:1常量:0];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:dimBackgroundUIView属性:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual-toItem:self.view属性:NSLayoutAttributeTop乘数:1常量:0];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:dimBackgroundUIView属性:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual-toItem:self.view属性:NSLayoutAttributeBottom乘数:1常量:0];
}
-(无效)添加约束表单查看{
[主视图设置TranslatesAutoResizengMaskintoConstraints:否];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:mainView属性:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual-toItem:self.view属性:NSLayoutAttributeLeft乘数:1常量:0];
[self.view addConstr