简体   繁体   English

如何在CAShape图层顶部显示UIImage?

[英]How can I display a UIImage on top of a CAShape Layer?

I'm having trouble drawing a UIImage in front of a CAShapeLayer . 我在CAShapeLayer前面绘制UIImage时遇到麻烦。 I have a vector image in the background with an icon in front of it. 我的背景中有一个矢量图像,前面带有一个图标。 The vector is important because I'm eventually scaling & animating this; 向量很重要,因为我最终要对此进行缩放和动画处理; and I have a ton of these onscreen so for performance I'd like to do this at a lower level than with a bunch of UIImageView subviews. 而且我在屏幕上有很多这样的显示,因此为了提高性能,我想在较低的水平上进行操作,而不是使用一堆UIImageView子视图。

I've tried: 我试过了:

  • Setting the container view's layer class to CAShapeLayer 将容器视图的图层类设置为CAShapeLayer
  • Adding a CAShapeLayer as a sublayer of my container view CAShapeLayer添加为容器视图的子层
  • Setting my container's layer and/or sublayer's content to the image's CGImage 将容器的图层和/或子图层的content设置为图像的CGImage
  • Manually drawing the image in the container view's drawRect (my Quartz-fu is pretty weak, though) 在容器视图的drawRect手动绘制图像(尽管我的Quartz-fu很弱)

In most cases I get the image drawn behind the shape (I vaguely understand that this is due to how CAShapeLayers composite themselves) or no image at all. 在大多数情况下,我会在形状后面绘制图像(我隐约地理解这是由于CAShapeLayers如何合成自身)或根本没有图像。 Surely this is possible without resorting to subviews? 当然可以在不诉诸子视图的情况下做到这一点?

Have you tried setting the zPosition of a sublayer to a positive value? 您是否尝试将子层的zPosition设置为正值? The default is 0.0, so making it larger than zero would push it "higher" in the hierarchy. 默认值为0.0,因此使其大于零会在层次结构中将其“推高”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM