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



我有一个以UIImageView呈现的图像,它占据了UIViewController的整个视图。 我想在这个UIImageView的子视图(UIView(层中绘制一个circle(fill: yellow)。 我的问题是,如何使这个黄色圆圈的不透明度使其可以看到背景图像(例如不透明度 0.2(。 我尝试了以下方法,但没有成功。 我得到的只是背景图像顶部的黄色实心圆圈。 我需要这个黄色圆圈位于所描述的子视图层中,而不是在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

最新更新