我想在中间遮罩我的视图,当我扫描条形码时,我想有一个透明的矩形(CGRect),而背景视图则是黑色且略微模糊。阅读其他文章后,我设法整理了一些应该可以使用的代码,但是我认为问题是它没有使maskLayer.fillRule = .evenOdd行的掩码反转,因为即使删除了该行,视觉效果也不会改变代码。
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
...
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = view.layer.bounds
previewLayer.videoGravity = .resizeAspectFill
view.layer.addSublayer(previewLayer)
let opaqueView = UIView()
opaqueView.frame = view.layer.bounds
opaqueView.backgroundColor =
UIColor.black.withAlphaComponent(0.7)
view.addSubview(opaqueView)
let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(roundedRect: CGRect(x: self.view.frame.width * 0.10, y: self.view.frame.height * 0.28, width: self.view.frame.width * 0.8, height: self.view.frame.height * 0.44), cornerRadius: self.view.frame.height * 0.01).cgPath
maskLayer.backgroundColor = UIColor.black.cgColor
maskLayer.fillRule = .evenOdd //NOT WORKING
opaqueView.layer.mask = maskLayer
opaqueView.clipsToBounds = true
captureSession.startRunning()