简体   繁体   English

如何在 iOS 中屏蔽 UIViews

[英]How to mask UIViews in iOS

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).我想使用灰色图像屏蔽 UIView(需要转换为 alpha 比例进行屏蔽)。 The UIView has background. UIView 有背景。 It should be easy to mask an image, but I want to mask any UIView.屏蔽图像应该很容易,但我想屏蔽任何 UIView。

Any clues will be appreciated.任何线索将不胜感激。

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.请注意,这里我们只需要 alpha 值,所有其他颜色都将被忽略,因此请确保您使用的方法支持 alpha 值。 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.在本例中,我从 .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.编辑:从 iOS8 开始,您现在可以简单地通过将另一个视图分配给其 maskView 属性来屏蔽视图。 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.一般规则保持不变,因为 maskView 的 alpha 层用于确定它所应用到的视图的不透明度。

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:结果:

示例截图

I don't know the exact code off the top of my head but the basic idea is to have two UIViews.我不知道我脑子里的确切代码,但基本的想法是有两个 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.一个 UIView 会将它的图像属性设置为灰度图像,而另一个 UIView 将像往常一样设置,唯一的区别是您将初始 UIView 直接放置在包含“正常”图像的 UIView 之上。

I hope that is enough to push your idea a step further.我希望这足以让你的想法更进一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM