如何在作为 UIImageView 的子视图的 UIView 层中绘制的形状中获得不透明度?

How to attain opacity in a shape drawn in a layer of UIView that is a subView of a UIImageView?

我在 UIImageView 中显示了一张图片,它占据了 UIViewController 的整个视图。我想在这个 UIImageView 的子视图(UIView)的图层中绘制一个 circle(fill: yellow)。我的问题如下,如何设置这个黄色圆圈的不透明度,使其可以看到背景图像(例如不透明度 0.2)。我尝试了以下但没有成功。我得到的只是背景图像顶部的实心黄色圆圈。我需要这个黄色圆圈位于 subView 的层中,而不是 UIImageView.

的层中
let dotSize: CGFloat = 30
var fourthDotView: UIView?
@IBOutlet weak var imgPreViewOutlet: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    fourthDotView = UIView.init()
    fourthDotView?.frame = CGRect.init(x: fourthOrigin.x, y: fourthOrigin.y, width: dotSize, height: dotSize)

    drawFourthDot()
    imgPreViewOutlet.addSubview(fourthDotView!)

}



// DRAW YELLOW CIRCLE
func drawFourthDot() -> Void {
    let layer = CAShapeLayer.init()
    layer.path = UIBezierPath.init(roundedRect: fourthDotView!.bounds, cornerRadius: 50).cgPath
    layer.fillColor = UIColor.init(red: 255, green: 255, blue: 0, alpha: 0.2).cgColor
    fourthDotView?.layer.addSublayer(layer)
}

要设置不透明度,您应该使用 opacity 属性。

layer.opacity = 0.20