我有一个CALayer (imageLayer)和一个子类CALayer,它充当CALayer上的掩码(maskLayer)。
我正在用手指在maskLayer上作画,这样我就可以暂时擦除imageLayer的一部分。
我通过保留一个位图上下文(maskContext)并在touchesMoved中给它添加一个笔划来实现这一点。所以我的maskContext只保存了我所绘制的所有笔画。在touchesMoved中,我做[maskLayer setNeedsDisplay];
在我的子类maskLayer中,我有drawInContext方法:
-(void)drawInContext:(CGContextRef)ctx
{
CGImageRef image = CGBitmapContextCreateImage(maskContext);
CGContextDrawImage(ctx, CGRectMake(0,0,self.frame.size.width, self.frame.size.height), image);
}这一切都运行得很好,但速度非常慢,因为它为每个touchesMoved事件绘制整个视图。
我想过使用[maskLayer setNeedsDisplayInRect:rect],但我想知道这是什么意思,因为drawInContext不看rect。我可以将rect传递给drawInContext,但只使用setNeedsDisplay就可以做到这一点。
对于CALayer,有没有什么是setNeedsDisplayInRect做的而setNeedsDisplay不做的?
(我知道如果我使用drawRect,它会接受定义的rect,但CALayer的drawInContext不会)
另外,有没有更好的方法通过手指绘画来完成这个蒙版呢?主要问题是CGContext不是画布,并且不会记住以前的笔划,因此它们必须一直被重新绘制。
发布于 2011-08-03 00:44:11
如果通过调用setNeedsDisplayInRect:来标记要显示的层,那么可以使用CGContextGetClipBoundingBox来标记要在drawInContext:中显示的区域。
https://stackoverflow.com/questions/5194527
复制相似问题