objective-c - 围绕形状、核心图形画一条笔画

标签 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

现在我想在刚刚绘制的十字旗周围画一条笔画

我应该做什么来实现这个目标。请就这个问题给我建议。 谢谢。

最佳答案

当然 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:

关于objective-c - 围绕形状、核心图形画一条笔画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13256436/

相关文章:

ios - 如何使用 NSDictionary 收集嵌套在数组中的 JSON 数据

Objective-C/ cocoa : Do I have a memory leak here?

objective-c - 如何处理 '[<__NSCFString 0x2f1730> valueForUndefinedKey:]: this class is not key value coding-compliant for the key $oid' 错误

ios - 运行时错误 : __NSAutoreleaseNoPool(): . ..autoreleased 没有适当的池 - 只是泄漏

objective-c - 使用 Core Graphics 强制窗口重绘自身?

ios - 检测贝塞尔曲线路径内的水龙头

iphone - Objective-c 同一 View 中的多个委托(delegate) - ECSlidingViewController

objective-c - ios_type ^=0x1,这是什么意思?

ios - 音频闪避后音乐变得更柔和?

swift - context.fill() 没有显示 Swift 4 Xcode 10