xcode 在 Swift 上的另一个 UIView 中使用 UIView 作为掩码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43668491/
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
Using an UIView as a Mask in another UIView on Swift
提问by DevRenanGaspar
I am developing an App on xCode with Swift. My screen has an Image of an animal (outlet "backImage"), over this image I have a view (outlet "coverView"), color black, which covers all the screen. So so far you can't see the animal image, because the "coverView" is in front of it. Then I have another view (outlet "maskView") which is smaller and it's over the big view "coverView". What I want is to use this "maskView" as mask and therefor see the "backImage" through it, like a window.
我正在使用 Swift 在 xCode 上开发应用程序。我的屏幕有一个动物的图像(出口“backImage”),在这个图像上我有一个视图(出口“coverView”),颜色为黑色,它覆盖了整个屏幕。到目前为止你还看不到动物图像,因为“coverView”在它的前面。然后我有另一个较小的视图(出口“maskView”),它位于大视图“coverView”上方。我想要的是使用这个“maskView”作为掩码,因此通过它看到“backImage”,就像一个窗口。
Is there anyone out there able to figure this out?
有没有人能够弄清楚这一点?
Here is my screen, I want to see the woman character behind the big gray view through the smaller white view:
这是我的屏幕,我想通过较小的白色视图看到大灰色视图后面的女性角色:
回答by Alexandre Lara
You can set the alpha
property from your mask view and add in front of the other view, for instance:
您可以alpha
从掩码视图设置属性并添加到另一个视图的前面,例如:
let maskView = UIView()
maskView.backgroundColor = UIColor(white: 0, alpha: 0.5) //you can modify this to whatever you need
maskView.frame = CGRect(x: 0, y: 0, width: imageView.frame.width, height: imageView.frame.height)
yourView.addSubview(maskView)
EDIT: Now that you edited your question with an image, now I see what you need, so here is how you can accomplish that.
编辑:既然你用图像编辑了你的问题,现在我知道你需要什么,所以这里是你如何完成它。
func setMask(with hole: CGRect, in view: UIView){
// Create a mutable path and add a rectangle that will be h
let mutablePath = CGMutablePath()
mutablePath.addRect(view.bounds)
mutablePath.addRect(hole)
// Create a shape layer and cut out the intersection
let mask = CAShapeLayer()
mask.path = mutablePath
mask.fillRule = kCAFillRuleEvenOdd
// Add the mask to the view
view.layer.mask = mask
}
With this function, all you need is to have a view and create a shape that it's going to be a hole in that view, for instance:
使用此功能,您只需要拥有一个视图并创建一个形状,该形状将成为该视图中的一个洞,例如:
// Create the view (you can also use a view created in the storyboard)
let newView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
newView.backgroundColor = UIColor(white: 0, alpha: 1)
// You can play with these values and find one that fills your need
let rectangularHole = CGRect(x: view.bounds.width*0.3, y: view.bounds.height*0.3, width: view.bounds.width*0.5, height: view.bounds.height*0.5)
// Set the mask in the created view
setMask(with: rectangularHole, in: newView)
回答by DevRenanGaspar
Thank you, @Alexandre Lara! You did it!
谢谢你,@Alexandre Lara!你做到了!
Here goes the solution:
解决方法如下:
@IBOutlet weak var windowView: UIView!
@IBOutlet weak var bigCoverView: UIView!
func setMask(with hole: CGRect, in view: UIView){
// Create a mutable path and add a rectangle that will be h
let mutablePath = CGMutablePath()
mutablePath.addRect(view.bounds)
mutablePath.addRect(hole)
// Create a shape layer and cut out the intersection
let mask = CAShapeLayer()
mask.path = mutablePath
mask.fillRule = kCAFillRuleEvenOdd
// Add the mask to the view
view.layer.mask = mask
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let rectangularHole = windowView.frame.integral
// Set the mask in the created view
setMask(with: rectangularHole, in: bigCoverView!)
}