【问题标题】:Quartz iPhone drawRect not called石英 iPhone drawRect 未调用
【发布时间】:2012-04-27 19:16:34
【问题描述】:

我正在继承 UIView 并通过详细视图控制器调用它。当我加载它时,它以正确的尺寸和位置显示,但它只是一个黑色方块......当我将 UIView 放入带有类的界面构建器时,它就可以工作。我不认为 draw rect 被称为。这是我的代码:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setNeedsDisplay];
    }
    return self;
}


- (void)drawRect:(CGRect)rect
{
    // Drawing code

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 255.0, 255.0, 0, 1);
    CGContextStrokeRect(context, CGRectMake(0, 0, 50, 50));
}

我这样称呼它:

 GameBox *boxy = [[GameBox alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
    [self.view addSubview: boxy];

【问题讨论】:

  • 你为什么在 init 中调用 setneedsdisplay?这是不正确的,在初始化时它还没有被添加到任何视图中,并且视图还没有加载..
  • 那我怎样才能让这个东西不显示为黑色呢?我试过没有设置setneedsdisplay
  • 首先颜色介于 0 和 1 之间,而不是 255...

标签: iphone quartz-graphics drawrect


【解决方案1】:

您正在设置填充颜色,但您正在抚摸一个矩形。需要设置描边颜色:CGContextSetRGBStrokeColor

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-18
    • 2012-11-04
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多