2011-01-21 72 views
0

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

- (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(); 
} 

回答

0

CGContextSetLineWidth()调用只影响调用CGContextStrokePath()。一个路径只包含每个段的坐标和类型,并且不包含有关线宽或模式或其他任何信息。如果您需要使用不同的线宽绘制每个线段,则需要使用单独的路径对CGContextStrokePath()进行单独调用。