首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在形状周围绘制笔触,核心图形

在形状周围绘制笔触,核心图形
EN

Stack Overflow用户
提问于 2012-11-07 01:44:34
回答 2查看 583关注 0票数 2

我画了一个形状,如下所示:

代码语言:javascript
复制
- (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);
}

我在下面看到了一面深浅不一的十字旗

现在我想在我刚刚画的十字旗周围画一笔。

我应该怎么做才能做到这一点。请在这个问题上给我一些建议。谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-07 01:51:16

当然CGContextDrawPath(context, kCGPathFillStroke);就是你要找的

您可以使用以下命令调整图案和颜色:

代码语言:javascript
复制
CGContextSetStrokePattern
CGContextSetStrokeColor

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

因此,在你的例子中,假设你想要一个纯黑色的笔触,你应该有:

代码语言:javascript
复制
- (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);
}

产生:

票数 2
EN

Stack Overflow用户

发布于 2012-11-07 03:43:18

代码语言:javascript
复制
- (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);

在尝试之后,我得到了以下结果

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13256436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档