swift - 在 UIImageView 中的 Image 后面插入 CAShapeLayer

标签 swift swift3 uiimageview uiimage cashapelayer

正如标题所说。我有一个 UIImageView,我正在将 CAShapeLayers 插入 View 中。问题是我不知道如何将它们插入 UIImageView 的图像后面。任何建议将不胜感激,谢谢。

最佳答案

您可以尝试进行一些图层重新排序,但您可能会被 UIImageView 自己的内部代码挫败。我的建议是创建一个容器 View 并在其中添加子图层,然后将 ImageView 添加为 subview (本质上也是将其添加为子图层)。只要图像具有透明度并且 UIImageView 不是不透明的,它就可以工作。 这段代码在 Playground 上对我有用:

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)

祝你好运!

关于swift - 在 UIImageView 中的 Image 后面插入 CAShapeLayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46185359/

相关文章:

ios - 链接器命令失败,退出代码为 1 xcode 6.4

swift - 为什么 SCNNode 使用 ARKit 到处移动?

ios - 是否可以在 Swift 的启动屏幕中显示额外的图像和按钮

xcode - 创建音频波可擦洗时间线。 swift 3 MacOS

android - 在 View 上显示图像

ios - 在 objc 类中使用 nib 名称初始化 swift View Controller

swift - 从 Swift 中的函数返回任何类型

ios - 在 viewDidLoad block UI 中对图像使用 GaussianBlur

ios - Swift 3 将图像从 URL 添加到 UIImageView

ios - 在 UINavigationController 上推送多个 View Controller 时出现内存问题