HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何(成功)将maskView设置为UIView?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在制作一个基于相机的应用程序,并希望在相机预览上制作一个磨砂覆盖图,从磨砂视图中切出一个圆角矩形.

我一直在努力用UIView的maskLayer实现这一点,在iOS 8中引入并且已经非常不成功.

这是我的代码目前的样子:

// 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)];
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说

看到这个的最好方法是使用一个装有RGBA图像的UIImageView,它运行一个alpha通道 – 从photoshop打开透明导出的PNG就可以了.然后调整大小,将原点设置为0,0.

第二种方法是创建一个UIView,没有alpha即[UIColor clearColor]并添加一个具有alpha [UIColor whiteColor]的子视图;

UIView *mask = [[UIView alloc] initWithFrame:(CGRect){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;

您可以使用[UIColor colorFromRed:green:blue:alpha:x]添加更多半透明xIV量的UIViews;

大佬总结

以上是大佬教程为你收集整理的ios – 如何(成功)将maskView设置为UIView?全部内容,希望文章能够帮你解决ios – 如何(成功)将maskView设置为UIView?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: