画一个CGlayer到另一个CGlayer

我使用两个CGlayers进行绘制,Layer1和Layer2,我绘制到Layer1和触摸结束,我将数据转移到Layer2,并清除Layer1,并在我的drawRect,方法,我画两个层的上下文。

点击删除button,我将绘图从Layer2转移到Layer1,然后我删除layer1,但触摸结束,我不转移到Layer2,因为我只擦除Layer1

在笔上点击如果擦除发生,我将绘图从Layer1转移到Layer2,否则以前的绘图可能会消失。

在代码中,它的样子

- (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); } 

所以我把这个图层从一个图层转移到另一个图层,但是会发生什么

1)如果我画一条线,那么我点击擦除button,然后不擦除,如果我再次点击笔画,并没有画任何东西,如果我再次点击擦除button,现在当我试图擦除整个绘图消失

2)但是,如果我画线,然后点击擦除button,并删除线,然后再转移到笔button,画一条线,再次转到擦除button,并尝试擦除,它完美的工作。