正如标题所说。我有一个UIImageView,并且正在将CAShapeLayers插入到视图中。问题是我不知道如何将它们插入UIImageView的图像后面。任何建议将不胜感激,谢谢。
您可以尝试对图层进行一些重新排序,但可能会被UIImageView自己的内部代码所挫败。我的建议是创建一个容器视图并在其中添加子层,然后将图像视图添加为子视图(实际上也将其添加为子层)。只要图像具有透明度并且UIImageView不是不透明的,它将起作用。这段代码在操场上为我工作:
let cOntainer= UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) let imageView = UIImageView(frame: container.bounds) imageView.image = UIImage(named: "duck.png") imageView.isOpaque = false let shape = CAShapeLayer() shape.path = UIBezierPath(roundedRect: container.bounds, cornerRadius: 2).cgPath shape.strokeColor = UIColor.black.cgColor shape.lineWidth = 10 shape.fillColor = UIColor.blue.cgColor shape.borderColor = UIColor.black.cgColor container.layer.addSublayer(shape) container.addSubview(imageView)
祝好运!