【问题标题】:drawRect is not working In IphonedrawRect 在 Iphone 中不起作用
【发布时间】:2010-11-02 12:14:50
【问题描述】:

嗨,我正在将 UIView 添加到窗口。

这个视图有drawRect as-

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

- (void)drawRect:(CGRect)rect {


        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetLineWidth(context, 2.0);

          CGContextMoveToPoint(context, 0, 0);
        CGContextAddLineToPoint(context, 300, 400);

        CGContextStrokePath(context);

}

但是这段代码没有画线。 所以有什么帮助吗??????

【问题讨论】:

  • 如果您只是在编辑时阅读问题框旁边的格式说明,您将为自己省去一个痛苦的世界。将代码缩进四个空格(并且不需要在每一行都使用<br>)。
  • 我不知道是否有必要,但调用CGContextSetStrokeColor... 函数之一可能是个好主意。

标签: iphone objective-c ios4


【解决方案1】:

使用

CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);

在您的代码之后。

【讨论】:

  • 我也试过这个 - CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(上下文, 2.0); CGContextMoveToPoint(上下文, 0, 0); CGContextAddLineToPoint(上下文, 300, 400); CGContextStrokePath(上下文); CGContextClosePath(上下文); CGContextDrawPath(上下文,kCGPathFillStroke);但它没有显示任何内容。
【解决方案2】:

我认为马塞洛·坎托斯是对的。您应该设置描边颜色。 也许你可以试试这个来设置颜色。

[[UIColor blackColor] set];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多