CALayer 子类“drawInContext”打电话但不画画?
我正在尝试绘制 CALayer 子类。 使用 setNeedsDisplay
调用 drawInContext
,但没有绘制任何内容。 这里做了什么/出了什么问题?
- (void)drawInContext:(CGContextRef)ctx
{
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
[[UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 100, 100)] fill];
[@"Vowel" drawAtPoint:CGPointMake(0, 0) withFont:[UIFont fontWithName:@"Chalkboard" size:14]];
}
编辑 我收到此错误:
CGContextAddPath:无效上下文 0x0
谢谢 沙尼
I am trying to draw in a CALayer subclass.
The drawInContext
is called with setNeedsDisplay
but nothing is drawn.
What am doing/getting wrong here ?
- (void)drawInContext:(CGContextRef)ctx
{
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
[[UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 100, 100)] fill];
[@"Vowel" drawAtPoint:CGPointMake(0, 0) withFont:[UIFont fontWithName:@"Chalkboard" size:14]];
}
Edit
I am getting this error :
CGContextAddPath: invalid context 0x0
Thanks
Shani
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在混合 CG 调用和 UIKit 调用。
-[UIBezierPath fill]
和-[NSString drawAtPoint:withFont:]
都绘制到 UIKit 上下文堆栈顶部的上下文中。这与传递到-drawInContext:
的上下文不同。您应该将您的函数修改为如下所示:You're mixing CG calls and UIKit calls.
-[UIBezierPath fill]
and-[NSString drawAtPoint:withFont:]
both draw into the context at the top of the UIKit context stack. That's not the same thing as the context passed into-drawInContext:
. You should modify your function to look like: