我使用两个 CGlayer 进行绘图,Layer1 和 Layer2,我绘制到 Layer1 并在触摸结束时,我将数据传输到 Layer2 并清除 Layer1,在我的 drawRect 方法中,我将两个层都绘制到上下文中。
单击擦除按钮时,我将绘图从 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)如果我画一条线,然后我点击擦除按钮,然后不擦除,如果我再次点击笔按钮并且没有绘制任何东西,如果我再次点击擦除按钮,现在当我尝试擦除整个绘图时消失
2)但是如果我画线,然后单击擦除按钮并擦除一条线,然后再次切换到笔按钮并绘制一条线,然后再次切换到擦除按钮并尝试擦除,它完美地工作。