我正在使用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