问题
I am using two CGlayers for drawing, Layer1 and Layer2, I draw into Layer1 and on touches end, I transfer the data into Layer2 and just clear Layer1, and in my drawRect, method, I draw both layers to context.
On erase button clicked, I transfer the drawing from Layer2 to Layer1 and then I erase layer1, but on touches end, I dont transfer it to Layer2, because I only erase Layer1
On pen clicked If erase has happened, I transfer drawing from Layer1 to Layer2, otherwise previous drawing can vanish.
In code , its like
- (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);
}
So I transfer the drawing from one Layer to another Layer this way, but what happens is
1) If I draw a line, then I click on erase button, then without erasing, if I again click on penbutton and without drawing anything, if I again click on erase button and now when I try to erase whole drawing disappears
2) But if I draw line, then click on erase button and erase a line, then again shift to pen button and draw a line and again shift to erase button and try to erase, it works perfectly.
来源:https://stackoverflow.com/questions/22201208/draw-one-cglayer-into-another-cglayer