【问题标题】:How to make a Drawing method in a View class work using CGContextRef?如何使用 CGContextRef 使 View 类中的绘图方法工作?
【发布时间】:2010-03-27 20:58:17
【问题描述】:

我在 View 类中有这两种方法。 drawRect 方法总是在视图初始化时被调用。但我无法让 drawLine 方法工作。当它被调用时它什么都不做。我应该处理 cgimagecontext 或类似的东西吗?请帮忙!!

- (void)drawRect:(CGRect)rect {
    // Drawing code
    // Drawing code
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    //CGContextSetRGBFillColor(contextRef, 0, 0, 255, 1);
    CGContextSetRGBStrokeColor(contextRef, 0, 0, 0, 1);
    CGContextSetLineWidth(contextRef, 5.0);
    CGContextBeginPath(contextRef);
    CGContextMoveToPoint(contextRef, 0, 0);
    CGContextAddLineToPoint(contextRef, 320, 480);
    CGContextStrokePath(contextRef);
}

    -(void)drawLine:(CGPoint)from to:(CGPoint) to {
    // Drawing code
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    //CGContextSetRGBFillColor(contextRef, 0, 0, 255, 1);
    CGContextSetRGBStrokeColor(contextRef, 0, 128, 0, 1);
    CGContextSetLineWidth(contextRef, 5.0);
    CGContextBeginPath(contextRef);
    CGContextMoveToPoint(contextRef, 0, 0);
    CGContextAddLineToPoint(contextRef, 320, 50);
    CGContextStrokePath(contextRef);

}

【问题讨论】:

    标签: iphone drawing cgcontext drawrect


    【解决方案1】:

    你是从 -drawRect 中调用 -drawLine 吗?您需要在 drawRect 方法中的视图中完成所有绘图。如果您从其他地方调用 -drawLine,它将不起作用。

    【讨论】:

      【解决方案2】:

      您只能在drawrect中进行绘图。如果您想通过您的 drawLine 方法绘制自定义线,请将 drawrect 中的硬编码点替换为变量。然后您可以在您的 drawLine 方法中设置这些变量,最后调用 [self setNeedsDisplay]。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-09
        相关资源
        最近更新 更多