我正在使用UIBezierPath
绘制Circle。我想在CGLayer
上绘制它,这样我可以在某些事件后通过缓存圆圈并在其上绘制文本(通过调用setNeedsDisplay
)。我应该如何在CGContextRef上绘制UIBezierPath。我下面如何在CGLayer上绘制UIBezierPath?
- (void)drawRect:(CGRect)rect
{
// Drawing code
static CGLayerRef sTextLayer = NULL;
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect textBounds = CGRectMake(0, 0, 200, 100);
if (sTextLayer == NULL) {
sTextLayer = CGLayerCreateWithContext(ctx, textBounds.size, NULL);
CGContextRef textCtx = CGLayerGetContext(sTextLayer);
CGContextSetRGBFillColor (textCtx, 1.0, 0.0, 0.0, 1);
UIGraphicsPushContext(textCtx);
// Draw circle
UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:textBounds];
[[UIColor blackColor] setFill];
circle.lineWidth = 2.0;
[circle fill];
UIGraphicsPopContext();
}
if (self.drawString) {
UIFont *font = [UIFont systemFontOfSize:13.0];
NSString *string = @"HAPPY BIRTHDAY";
[string drawInRect:textBounds withFont:font];
}
}
您可以查看示例代码并在您的项目中实现 – Sport