【问题标题】:CALayer subclass "drawInContext" called but not drawing?CALayer子类“drawInContext”调用但不绘图?
【发布时间】:2012-02-17 15:29:19
【问题描述】:

我正在尝试绘制 CALayer 子类。 drawInContextsetNeedsDisplay 调用,但没有绘制任何内容。 这里在做什么/出了什么问题?

 - (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

谢谢 沙尼

【问题讨论】:

标签: ios calayer quartz-2d


【解决方案1】:

您正在混合 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();
}

【讨论】:

  • 这似乎是正确的答案,应该被接受。我遇到了与提交者相同的问题,并按照建议将代码封装在 UIGraphicsPushContext/UIGraphicsPopContext 调用之间,修复了它。
  • 这对我有用,花了很多时间才找到正确的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多