Draw one CGlayer into another CGlayer

I use two CGlayers for drawing, Layer1 and Layer2, I draw in Layer1 and ending, I transfer the data to Layer2 and just clear Layer1, and in my drawRect method I draw both layers in context.

When I click on the erase button, I transfer the drawing from Layer2 to Layer1, and then erase layer1, but when done, I don’t transfer it to Layer2, because I only erase Layer1

When you tap on the pen. If erasure occurs, I transfer the pattern from Layer1 to Layer2, otherwise the previous pattern may disappear.

In code, for example,

- (void)eraserButtonClicked
{   

   CGContextRef layerContext1 = CGLayerGetContext( self.layer1);
   CGContextClearRect(layerContext1, self.bounds);
   CGContextDrawLayerInRect(layerContext1, self.bounds, self.layer2);

   CGContextRef layerContext = CGLayerGetContext(self.layer2 );
   CGContextClearRect(layerContext, self.bounds); 
}


- (void)pen1ButtonClicked
{
   CGContextRef layerContext = CGLayerGetContext(self.layer2 );
   CGContextClearRect(layerContext, self.bounds);
   CGContextDrawLayerInRect(layerContext, self.bounds, self.layer1);
   CGContextRef layerContext1 = CGLayerGetContext( self.layer1);
   CGContextClearRect(layerContext1, self.bounds);         


}

Therefore, I transfer the drawing from one layer to another layer in this way, but what happens is

1) , , , penbutton , , , ,

2) , , , .

+1

Source: https://habr.com/ru/post/1783405/


All Articles