在 Swift 上将一个 UIView 用作另一个 UIView 中的遮罩
Using an UIView as a Mask in another UIView on Swift
我正在使用 Swift 在 xCode 上开发应用程序。我的屏幕上有一个动物图像(插座 "backImage"),在这张图像上我有一个视图(插座 "coverView"),颜色为黑色,覆盖了整个屏幕。所以到目前为止你还看不到动物图像,因为 "coverView" 在它前面。然后我有另一个视图(出口 "maskView"),它更小,它在大视图 "coverView" 之上。我想要的是使用这个 "maskView" 作为掩码,因此通过它看到 "backImage",就像 window.
有没有人能解决这个问题?
这是我的屏幕,我想通过较小的白色视图看到灰色大视图后面的女性角色:
您可以从掩码视图中设置 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)
编辑:现在您已经用图片编辑了您的问题,现在我知道您需要什么,下面是您如何完成它。
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
}
使用此功能,您只需拥有一个视图并创建一个形状,它将成为该视图中的一个洞,例如:
// 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)
谢谢@Alexandre Lara!你做到了!
解决方案如下:
@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!)
}
我正在使用 Swift 在 xCode 上开发应用程序。我的屏幕上有一个动物图像(插座 "backImage"),在这张图像上我有一个视图(插座 "coverView"),颜色为黑色,覆盖了整个屏幕。所以到目前为止你还看不到动物图像,因为 "coverView" 在它前面。然后我有另一个视图(出口 "maskView"),它更小,它在大视图 "coverView" 之上。我想要的是使用这个 "maskView" 作为掩码,因此通过它看到 "backImage",就像 window.
有没有人能解决这个问题?
这是我的屏幕,我想通过较小的白色视图看到灰色大视图后面的女性角色:
您可以从掩码视图中设置 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)
编辑:现在您已经用图片编辑了您的问题,现在我知道您需要什么,下面是您如何完成它。
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
}
使用此功能,您只需拥有一个视图并创建一个形状,它将成为该视图中的一个洞,例如:
// 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)
谢谢@Alexandre Lara!你做到了!
解决方案如下:
@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!)
}