在形状,核心图形周围画一个笔划

时间:2012-11-06 17:44:34

标签: objective-c ios core-graphics drawrect

我正在绘制如下形状:

- (void)drawRect:(CGRect)rect
{
    // Draw a cross rectagle
    CGContextRef    context     =   UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextMoveToPoint(context, 190, 0);
    CGContextAddLineToPoint(context, 220, 0);
    CGContextAddLineToPoint(context, 310, 90);
    CGContextAddLineToPoint(context, 310, 120);
    CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);
    CGContextFillPath(context);
    CGContextRestoreGState(context);
}

我在

下面有一个浅黑色的十字旗

enter image description here

现在我想在我刚刚绘制的十字旗上画一个笔画

我该怎么做才能做到这一点。请就此问题向我提出建议。 感谢。

2 个答案:

答案 0 :(得分:2)

当然CGContextDrawPath(context, kCGPathFillStroke);就是你所追求的

您可以使用以下方式调整图案和颜色:

CGContextSetStrokePattern
CGContextSetStrokeColor

https://developer.apple.com/library/ios/#documentation/graphicsimaging/reference/CGContext/Reference/reference.html

因此,在您的情况下,假设您想要一个简单的黑色笔画,您将拥有:

- (void)drawRect:(CGRect)rect
{
    CGContextRef    context     =   UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);

    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);

    CGContextMoveToPoint(context, 190, 0);
    CGContextAddLineToPoint(context, 220, 0);
    CGContextAddLineToPoint(context, 310, 90);
    CGContextAddLineToPoint(context, 310, 120);
    CGContextClosePath(context);

    CGContextDrawPath(context, kCGPathFillStroke);
    CGContextFillPath(context);

    CGContextRestoreGState(context);
}

产地:

Reminds me of Kraftwork! Result of drawRect:

答案 1 :(得分:0)

- (void)drawRect:(CGRect)rect
{
    // Draw a cross rectagle
    CGContextRef    context     =   UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);

    //New
    CGContextSetLineWidth(context, 2.0);

    CGContextMoveToPoint(context, 190, 0);
    CGContextAddLineToPoint(context, 220, 0);
    CGContextAddLineToPoint(context, 310, 90);
    CGContextAddLineToPoint(context, 310, 120);

    //New
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);

    CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);
    CGContextFillPath(context);

    //New
    CGContextStrokePath(context);

    CGContextRestoreGState(context);
}

@WDUK:花了好几个小时才搞清楚,我知道为什么你的上述答案不起作用。 原因是,当您首先执行CGContextFillPath时,路径最终会被清除,然后您再也无法对其进行CGContextStrokePath。 因此,为了CGContextFillPathCGContextStrokePath,我们必须

CGContextDrawPath(context,  kCGPathFillStroke);

尝试后,我得到以下

enter image description here

相关问题