简体   繁体   English

如何在iOS中使用addSublayer和renderInContext

[英]How will work addSublayer and renderInContext in iOS

When i have written code as following way.. 当我按照以下方式编写代码时..

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(context, kCGBlendModeDifference);

    sublayer = [CALayer layer];
    sublayer.backgroundColor = [UIColor redColor].CGColor;
    sublayer.opaque = NO;
    sublayer.frame = CGRectMake(30, 30, 250, 200);
    [sublayer renderInContext:context];

    sublayer1 = [CALayer layer];
    sublayer1.opaque = NO;
    sublayer1.backgroundColor = [UIColor greenColor].CGColor;
    sublayer1.frame = CGRectMake(120, 120, 100, 250);
    [sublayer1 renderInContext:context];

}

I got as following first image as 我得到了第一张图片

在此输入图像描述在此输入图像描述

The positions of sublayer, sublayer1 are not correct way because i have set frames to them. 子层,子层1的位置不正确,因为我已经设置了帧。 The intersection of them is blended color. 它们的交叉点是混色。 Why are they not set the actual position? 他们为什么没有设定实际位置?

But when i use addSublayer , i got as above second image. 但是当我使用addSublayer ,我得到了第二张图像。 Here the positions of sublayer, sublayer1 are correct way. 这里子层,子层1的位置是正确的方式。 The intersection of them is not blended color. 它们的交叉点不是混合色。 What happened like that. 发生了什么事。

Why i created sublayer is, when i drag sublayers the intersection part of them has to be blended color. 为什么我创建子图层,当我拖动子图层时,它们的交叉部分必须是混合颜色。 Please anyone explain to me. 请有人向我解释。

Thanks in Advance. 提前致谢。

This is what happens in your code: In the first case, you just drew the layers in the graphics context of the view's underlying layer. 这就是代码中发生的情况:在第一种情况下,您只是在视图底层的图形上下文中绘制了图层。 That's what drawRect does. 这就是drawRect所做的。 The origin of the frame doesn't affect that because it just positions sublayers inside their parent layer. 框架的原点不会影响它,因为它只是将子图层放在其父图层中。 But here, the layer is never added to the parent layer, so the origin does nothing at all and both shapes appear at 0, 0. 但是在这里,图层永远不会添加到父图层,因此原点根本不做任何事情,两个图形都显示在0,0处。

In the second case, first the exact same thing happened as before, but because you also added the layers to the layer hierarchy, they got rendered again on top of the view's underlying layer, at the specified position. 在第二种情况下,首先发生与以前完全相同的事情,但是因为您还将图层添加到图层层次结构中,所以它们再次在视图的底层图层上,在指定位置呈现。 Each sublayer is rendered in it's own context, so the blend mode you set in drawRect has no effect on them. 每个子图层都在其自己的上下文中呈现,因此您在drawRect中设置的混合模式对它们没有影响。

Actually you don't need sublayers at all if you just want to get an image of the two rectangles. 实际上,如果您只想获得两个矩形的图像,则根本不需要子图层。 You can draw them directly in drawRect in the view's underlying layer: 您可以直接在视图的底层中的drawRect中绘制它们:

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(ctx, kCGBlendModeDifference);

    CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
    CGRect rect1 = CGRectMake(30, 30, 250, 200);
    CGContextFillRect(ctx, rect1);

    CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor);
    CGRect rect2 = CGRectMake(120, 120, 100, 250);
    CGContextFillRect(ctx, rect2);
}

Edit (because the question was edited): 编辑(因为问题已编辑):

You can use this approach for dragging layers like this: 您可以使用此方法拖动图层,如下所示:

  • Draw a rectangle for each moving layer on a new layer or view on top of them, with the blend mode and drawing commands described above 使用上述混合模式和绘图命令,在新图层或顶部视图上为每个移动图层绘制一个矩形
  • Use the current frames of the moving layers for the drawing rects 使用移动图层的当前帧作为绘图
  • Make sure that the new top layer doesn't receive touches 确保新的顶层没有接收到触摸
  • Redraw the new layer whenever the position of an interactive layer is changed 每当交互层的位置发生变化时,重新绘制新图层

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

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