Ios 如何(成功)将maskView设置为UIView?
我正在制作一个基于相机的应用程序,我想在相机预览上做一个磨砂覆盖,从磨砂视图中剪出一个圆角矩形 我一直试图通过iOS8引入的UIView的maskLayer来实现这一点,但一直没有成功 下面是我的代码当前的样子:Ios 如何(成功)将maskView设置为UIView?,ios,objective-c,uiview,ios8,mask,Ios,Objective C,Uiview,Ios8,Mask,我正在制作一个基于相机的应用程序,我想在相机预览上做一个磨砂覆盖,从磨砂视图中剪出一个圆角矩形 我一直试图通过iOS8引入的UIView的maskLayer来实现这一点,但一直没有成功 下面是我的代码当前的样子: // Blur the preview UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; UIView *blurView = [[UIVisualEffectView alloc
// Blur the preview
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIView *blurView = [[UIVisualEffectView alloc] initWithEffect:effect];
blurView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view insertSubview:blurView aboveSubview:imagePicker.view];
// Make blur view fill screen
{
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[blurView]-0-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(blurView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[blurView]-0-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(blurView)]];
}
// Add a rounded of transparency to the blurView **not working**
UIView *mask = [[UIView alloc] initWithFrame:CGRectMake(50.f, 50.f, 50.f, 50.f)];
mask.alpha = 1.0f;
mask.backgroundColor = [UIColor redColor];
[blurView setMaskView:mask]; // Remove this line, and everything is frosted. Keeping this line and nothing is frosted.
正如我的评论所指出的,添加遮罩会完全删除磨砂视图(更改遮罩的不透明度不会产生任何效果)。我知道这不会产生我想要的效果(它应该只在那个矩形中磨砂),但我无法让maskView以任何容量工作。UIView api说
一种可选视图,其alpha通道用于屏蔽视图的
内容
最好的方法是使用一个加载了RGBA图像的UIImageView,该图像带有一个alpha通道——一个从photoshop导出的透明度打开的PNG就可以了。然后调整大小,原点设置为0,0
第二种方法是创建一个不带alpha的UIView,即[UIColor clearColor]
,并添加一个带有alpha[UIColor whiteColor]的子视图代码>
UIView *mask = [[UIView alloc] initWithFrame:(CGRect){0,0,100,100}];
mask.backgroundColor = [UIColor clearColor];
UIView *areaToReveal = [[UIView alloc] initWithFrame:(CGRect){20,20,50,50}];
areaToReveal.backgroundColor = [UIColor whiteColor];
[mask addSubview:areaToReveal];
_myView.maskView = mask;
您可以使用[uicolorfromred:green:blue:alpha:x]添加更多半透明的UIView代码>代码的最后一行包含错误。Try:[blurView setMaskView:mask]@这是我帖子中的一个错误(现已更正)。不过,感谢您指出这一点。如果您将[UIColor whiteColor]添加到代码中会更正确,因为它基于新UIView的默认行为,而苹果决定更改它时,这可能会出现问题。无论如何,这是正确的。