2010-03-10 73 views
2

更新: 我嘗試實施Peter指定的方法,並且得到不正確的陰影。哪裏不對?使用Core Graphics在CGContextClip後繪製陰影

alt text http://grab.by/2XyP

CGContextSetShadowWithColor(c, CGSizeMake(4, 4), kAudioThumbShadowBlur, [[UAColor blackColor] CGColor]); 
    CGContextFillPath(c); 

    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathMoveToPoint(path, NULL, minx, midy); 
    CGPathAddArcToPoint(path, NULL, minx, miny, midx, miny, kDefaultMargin); 
    CGPathAddArcToPoint(path, NULL, maxx, miny, maxx, midy, kDefaultMargin); 
    CGPathAddArcToPoint(path, NULL, maxx, maxy, midx, maxy, kDefaultMargin); 
    CGPathAddArcToPoint(path, NULL, minx, maxy, minx, midy, kDefaultMargin); 
    CGPathCloseSubpath(path); 


    // Fill and stroke the path 
    CGContextSaveGState(c); 
    CGContextAddPath(c, path); 
    CGContextClip(c); 


    myGradient = CGGradientCreateWithColorComponents(myColorspace, components, locations, 2); 
    CGContextDrawLinearGradient(c, myGradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), 0); 

    CGContextAddPath(c, path); 
    CGPathRelease(path); 
    CGContextStrokePath(c); 
    CGContextRestoreGState(c); 





原題: 我期待周圍畫一個自定義的底部,圓形的單元項自然的影子,我做在使用此代碼CoreGraphics中:

... 
    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathMoveToPoint(path, NULL, minx, miny); 
    CGPathAddArcToPoint(path, NULL, minx, maxy, midx, maxy, kDefaultMargin); 
    CGPathAddArcToPoint(path, NULL, maxx, maxy, maxx, miny, kDefaultMargin); 
    CGPathAddLineToPoint(path, NULL, maxx, miny); 
    CGPathAddLineToPoint(path, NULL, minx, miny); 
    CGPathCloseSubpath(path); 

    // Fill and stroke the path 
    CGContextSaveGState(c); 
    CGContextAddPath(c, path); 
    CGContextClip(c); 

    myGradient = CGGradientCreateWithColorComponents(myColorspace, components, locations, 2); 
    CGContextDrawLinearGradient(c, myGradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), 0); 

    CGContextAddPath(c, path); 
    CGPathRelease(path); 
    CGContextStrokePath(c); 
    CGContextRestoreGState(c); 
... 

我想要在漸變填充之前或之後應用此路徑外部的陰影。做這件事的最好方法是什麼?

回答

9

漸變繪製不算作填充,因此首先設置陰影並進行純色填充。然後,繪製漸變和剪裁筆畫的純色填充。

+0

這是有道理的,但陰影在曲線路徑下會是直的嗎?我正在尋找陰影,以便在彎曲的路徑下自然彎曲 – coneybeare 2010-03-10 04:07:56

+0

爲什麼在彎曲的填充下它會筆直(我假設你是指矩形)? – 2010-03-10 04:18:12

+0

我想我很困惑:)試用後,它效果很好。謝謝 - 馬特 – coneybeare 2010-03-10 04:24:57