2011-01-21 31 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()進行單獨調用。