简体   繁体   English

如何在Objective-C中设置显示顺序

[英]How to set order of display in Objective-C

I'm trying to mix between UIImageView drawing and line drawing using CGContext path draw commands, but the draw order is not correct. 我正在尝试使用CGContext路径绘制命令在UIImageView绘制和线条绘制之间进行混合,但是绘制顺序不正确。 I need to draw the path on top of the images but get the reverse. 我需要在图像的顶部绘制路径,但得到相反的结果。

The way I implemented it: 我的实现方式:

  1. Setting UIImage within a UIImageView and adding the UIImageView as a subview of the main view. 在UIImageView中设置UIImage并将UIImageView添加为主视图的子视图。
  2. Next I use the 'drawRect' (refresh draw function) to draw the path using the UIKit CGContextStrokePath function. 接下来,我将使用UIKit CGContextStrokePath函数使用“ drawRect”(刷新绘制函数)绘制路径。

Now, other than using the Context image draw function directly (I want to avoid that because I reuse my image resources by several UIImageViews), is there a way to set the order correctly (a flag set maybe)? 现在,除了直接使用Context图像绘制功能之外(我想避免这种情况,因为我通过多个UIImageViews重用了我的图像资源),还有没有办法正确设置顺序(也许设置了标志)?

The problem again is that lines/path are drawn before the UIImageViews (which are drawn automatically via the connection to the parent UIView), hence they are not visible over the images / UIImageViews. 再次出现的问题是,线/路径是在UIImageViews之前绘制的(通过与父UIView的连接自动绘制),因此它们在图像/ UIImageViews上不可见。

Thanks 谢谢

The contents of the topmost (in this case, last added) subview will appear in front of all other views. 最顶层(在本例中为最后添加)子视图的内容将出现在所有其他视图的前面。 If you add an image subview and draw into its parent view, the image will appear in front of any drawing you do in that parent view. 如果添加图像子视图并绘制到其父视图中,则该图像将出现在您在该父视图中所做的任何图形的前面。

You should draw into a new view you add to the main view only after adding the UIImageView. 仅在添加UIImageView之后,才应绘制添加到主视图的新视图。

You could also use bringSubviewToFront: and sendSubviewToBack: to manually reorder views if you need to. 如果需要,您也可以使用bringSubviewToFront:sendSubviewToBack:手动重新排列视图。

(From your post, it's unclear which view you're drawing into, but I assume you're drawing into that main parent view which would explain this behavior.) (从您的帖子中,尚不清楚您正在绘制到哪个视图,但是我认为您正在绘制到可以解释此行为的主父视图。)

You can try implementing this through Quartz Core using layers (see the CALayer class documentation). 您可以尝试使用层通过Quartz Core实现此功能(请参见CALayer类文档)。 Basically, you can have layers hierarchies. 基本上,您可以具有图层层次结构。 You associate each UIView to a different layer, then the layers are rendered together providing a single, composite layer. 您将每个UIView关联到一个不同的层,然后将这些层渲染在一起,从而提供一个单独的复合层。 Besides, you can also apply transforms and animations to layers. 此外,您还可以将变换和动画应用于图层。

You need to import the QuartzCore header and do something like 您需要导入QuartzCore标头并执行类似的操作

#import <QuartzCore/QuartzCore.h>
UIView *mainView = [[UIView alloc] initWithFrame...
UIImageView *imageView = ...

CaLayer *mainViewLayer = mainView.layer;
[mainViewLayer addSubLayer: imageView.layer];

Then, when mainView appears on the screen, all the sublayers are merged together and rendered on screen. 然后,当mainView出现在屏幕上时,所有子层将合并在一起并在屏幕上呈现。 What happens is that each view renders its layer, while mainViewLayer is rendered merging together the two layers. 发生的是每个视图都呈现其层,而mainViewLayer呈现为将两个层合并在一起。

Let me know if this works for you. 让我知道这是否适合您。

You can have as many layers as you like. 您可以根据需要设置任意多个图层。 You can create an arbitrary hierarchi by using the CALayer methods 您可以使用CALayer方法创建任意层次结构

– addSublayer: 
– removeFromSuperlayer
– insertSublayer:atIndex:
– insertSublayer:below:
– insertSublayer:above:
– replaceSublayer:with:

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

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