CALayer 子类“drawInContext”打电话但不画画?

发布于 2024-12-29 01:35:59 字数 546 浏览 1 评论 0原文

我正在尝试绘制 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌若浮生 2025-01-05 01:35:59

您正在混合 CG 调用和 UIKit 调用。 -[UIBezierPath fill]-[NSString drawAtPoint:withFont:] 都绘制到 UIKit 上下文堆栈顶部的上下文中。这与传递到 -drawInContext: 的上下文不同。您应该将您的函数修改为如下所示:

- (void)drawInContext:(CGContextRef)ctx {
    UIGraphicsPushContext(ctx);
    [[UIColor redColor] setFill];
    [[UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 100, 100)] fill];
    [@"Vowel" drawAtPoint:CGPointMake(0, 0) withFont:[UIFont fontWithName:@"Chalkboard" size:14]];
    UIGraphicsPopContext();
}

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:

- (void)drawInContext:(CGContextRef)ctx {
    UIGraphicsPushContext(ctx);
    [[UIColor redColor] setFill];
    [[UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 100, 100)] fill];
    [@"Vowel" drawAtPoint:CGPointMake(0, 0) withFont:[UIFont fontWithName:@"Chalkboard" size:14]];
    UIGraphicsPopContext();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文