绘制子路径时CG更改/保存上下文

时间:2011-01-21 22:40:51

标签: iphone ios core-graphics

如果我正在做一个简单的绘图,并且我想更改某些子路径的线宽/短划线,如何在不改变所有路径的情况下实现此目的?我使用CGContextSaveGState(context)尝试了一些变体;但不能完全正确。这必须是完全不同的路径吗?我真的希望他们不要看着阴影。

- (void)drawPitch:(CGContextRef)context {

    // Push the context onto the stack
    UIGraphicsPushContext(context);

    //Reasonable defaults
    CGRect          pitchRect = CGRectMake(10, 10, 220, 344);
    CGSize          myShadowOffset = CGSizeMake(0,1);
    float           myColorValues[] = {0, 0, 0, 0.75};
    CGColorRef      myColor;
    CGColorSpaceRef myColorSpace;

    //Color Space
    myColorSpace = CGColorSpaceCreateDeviceRGB ();
    myColor = CGColorCreate (myColorSpace, myColorValues);

    // Set Stroke
    CGContextSetRGBStrokeColor(context, 1, 1, 1, 0.9);

    CGContextSetLineWidth(context, 4);

    // Pitch Outline at width:4
    CGContextAddRect(context, pitchRect);

    CGContextSaveGState(context);

    // Want this to be set width:2 just for the subpath
    CGContextSetLineWidth(context, 2);

    CGContextMoveToPoint(context, CGRectGetMinX(pitchRect), CGRectGetMinY(pitchRect) + (pitchRect.size.height * 0.50));
    CGContextAddLineToPoint(context, CGRectGetMaxX(pitchRect), CGRectGetMinY(pitchRect) + (pitchRect.size.height * 0.50));

    CGContextRestoreGState(context);


    // Set Line Shadow
    CGContextSetShadowWithColor(context, myShadowOffset, 10, myColor);


    // Stroke path
    CGContextStrokePath(context);


    // Pop the contect back on the stack
    UIGraphicsPopContext();
}

1 个答案:

答案 0 :(得分:0)

CGContextSetLineWidth()的通话仅影响对CGContextStrokePath()的通话。路径仅包含每个段的坐标和类型,并且不包含有关线宽或图案或其他任何内容的信息。如果您需要使用不同的线宽绘制每个线段,则需要使用单独的路径单独调用CGContextStrokePath()