Ale*_*one 3 iphone core-graphics objective-c calayer ios
我有一个使用此方法绘制自身的简单滚动图。我正在寻找一种向该图形添加文本标签的方法,但是找不到如何执行此操作。
我试过了:
[@"test" drawInRect: CGRectMake(0, 0, 10, 10) withFont:[UIFont systemFontOfSize:8]];
Run Code Online (Sandbox Code Playgroud)
我收到错误消息,指出上下文0x0无效。如何修改下面的代码以将文本绘制代码指向正确的上下文?
-(void)drawLayer:(CALayer*)l inContext:(CGContextRef)context
{
// Fill in the background
CGContextSetFillColorWithColor(context, graphBackgroundColor());
CGContextFillRect(context, layer.bounds);
// Draw the grid lines
// DrawGridlines(context, 0.0, 32.0);
// Draw the graph
CGPoint lines[64];
int i;
// X
for(i = 0; i < 32; ++i)
{
lines[i*2].x = i;
lines[i*2].y = -(xhistory[i] * 480.0)-10;
lines[i*2+1].x = i + 1;
lines[i*2+1].y = -(xhistory[i+1] * 480.0)-10;
}
CGContextSetStrokeColorWithColor(context, graphZColor());
CGContextStrokeLineSegments(context, lines, 64);
}
Run Code Online (Sandbox Code Playgroud)
对于每个线程,UIKit都会维护一堆图形上下文。您可以通过调用获取位于当前线程堆栈顶部的上下文UIGraphicsGetCurrentContext。
使用时drawInRect:withFont:,字符串会将自身吸引到by返回的上下文UIGraphicsGetCurrentContext,因此您需要UIGraphicsGetCurrentContext返回using 的context参数。完成绘制后,您必须致电。从而:drawLayer:inContext:UIGraphicsPushContextUIGraphicsPopContext
-(void)drawLayer:(CALayer*)l inContext:(CGContextRef)context {
UIGraphicsPushContext(context); {
// Fill in the background
CGContextSetFillColorWithColor(context, graphBackgroundColor());
CGContextFillRect(context, layer.bounds);
// etc.
} UIGraphicsPopContext();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2798 次 |
| 最近记录: |