如何在 iOS 中屏蔽 UIViews
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4559743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to mask UIViews in iOS
提问by subchap
I've seen similar questions, but haven't found workable answers.
我见过类似的问题,但没有找到可行的答案。
I want to mask a UIView using a grey image (need to convert to alpha scale for masking). The UIView has background. It should be easy to mask an image, but I want to mask any UIView.
我想使用灰色图像屏蔽 UIView(需要转换为 alpha 比例进行屏蔽)。UIView 有背景。屏蔽图像应该很容易,但我想屏蔽任何 UIView。
Any clues will be appreciated.
任何线索将不胜感激。
回答by Ash
I've been working on this problem for a couple of hours and have a solution that I think will do what you want. First, create your masking image using whatever means you see fit. Note that we only need the alpha values here, all other colours will be ignored, so make certain that the method you use supports alpha values. In this example I'm loading from a .png file, but don't try it with .jpg files as they don't have alpha values.
我已经在这个问题上工作了几个小时,并且有一个我认为可以满足您要求的解决方案。首先,使用您认为合适的任何方式创建您的遮罩图像。请注意,这里我们只需要 alpha 值,所有其他颜色都将被忽略,因此请确保您使用的方法支持 alpha 值。在本例中,我从 .png 文件加载,但不要尝试使用 .jpg 文件,因为它们没有 alpha 值。
Next, create a new layer, assign your mask to its contents and set this new layer to your UIView's own layer, like so: you should find that this masks the UIView and all its attached subviews:
接下来,创建一个新层,为它的内容分配遮罩,并将这个新层设置为您的 UIView 自己的层,如下所示:您应该会发现这遮罩了 UIView 及其所有附加的子视图:
UIImage *_maskingImage = [UIImage imageNamed:@"mask"];
CALayer *_maskingLayer = [CALayer layer];
_maskingLayer.frame = theView.bounds;
[_maskingLayer setContents:(id)[_maskingImage CGImage]];
[theView.layer setMask:_maskingLayer];
With this done, you can set the UIView's background colour to whatever you like and the mask will be used to create a coloured filter.
完成后,您可以将 UIView 的背景颜色设置为您喜欢的任何颜色,并且遮罩将用于创建彩色过滤器。
EDIT:As of iOS8 you can now mask a view simply by assigning another view to its maskView property. The general rules stay the same in that the maskView's alpha layer is used to determine the opacity of the view it is applied to.
编辑:从 iOS8 开始,您现在可以简单地通过将另一个视图分配给其 maskView 属性来屏蔽视图。一般规则保持不变,因为 maskView 的 alpha 层用于确定它所应用到的视图的不透明度。
回答by Stonetip
For apps targeting iOS 8.0+ this worked well (in this case, using a gradient as the mask) It avoids any need to resize or position the mask.
对于面向 iOS 8.0+ 的应用程序,这效果很好(在这种情况下,使用渐变作为蒙版)它避免了调整蒙版大小或定位蒙版的任何需要。
// Add gradient mask to view
func addGradientMask(targetView: UIView)
{
let gradientMask = CAGradientLayer()
gradientMask.frame = targetView.bounds
gradientMask.colors = [UIColor.blackColor().CGColor, UIColor.clearColor().CGColor]
gradientMask.locations = [0.8, 1.0]
let maskView: UIView = UIView()
maskView.layer.addSublayer(gradientMask)
targetView.maskView = maskView
}
In my case, I want to remove the mask once the user starts scrolling. This is done with:
就我而言,我想在用户开始滚动时移除遮罩。这是通过以下方式完成的:
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
exerDetailsTableView.maskView = nil
}
where the view is defined as an @IBOutlet:
其中视图定义为@IBOutlet:
@IBOutlet weak var exerDetailsTableView: UITableView!
Result:
结果:
回答by Jealous Republic
I don't know the exact code off the top of my head but the basic idea is to have two UIViews. One UIView would have it's image property set to be the grey scale image and the other UIView would be set as usual the only difference is that you would position the initial UIView directly on top of the UIView containing the "normal" image.
我不知道我脑子里的确切代码,但基本的想法是有两个 UIViews。一个 UIView 会将它的图像属性设置为灰度图像,而另一个 UIView 将像往常一样设置,唯一的区别是您将初始 UIView 直接放置在包含“正常”图像的 UIView 之上。
I hope that is enough to push your idea a step further.
我希望这足以让你的想法更进一步。