2012-07-29 30 views
1

我喜歡畫畫的玻璃有幾個要素 - 頂部橢圓 - 底部橢圓 - 和它們之間隔CGContextAddEllipse - GET重疊短促 - 石英

接下來的線條,應該用漸變填充。元素的工作,但在這一點上,玻璃的中間與頂部或底部的橢圓接觸,該區域被剪切。

- (void)drawRect:(CGRect)rect 
{ 
    CGPoint c = self.center;  
    // Drawing code 
    CGContextRef cx = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(cx, 1.0); 
    [[UIColor whiteColor] setStroke]; 

    // DrawTheShapeOfTheGlass 
    CGContextBeginPath(cx); 
    // Top and Bottom Ellipse 
    CGContextAddEllipseInRect(cx, CGRectMake(0, 0, 100, 20)); 
    CGContextAddEllipseInRect(cx, CGRectMake(10, 90, 80, 20)); 
    // Define the points for the Area inbetween 
    CGPoint points[] = { {0.0,10.0},{10.0,100.0},{90.0,100.0},{100.0,10.0} }; 
    CGContextAddLines(cx, points, 4); 
    CGContextClosePath(cx); 
    // Clip, that's only the Clipped-Area wil be filled with the Gradient 
    CGContextClip(cx); 

    // CreateAndDraw the Gradient 
    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGFloat colorSpace[] = {1.0f, 1.0f, 1.0f, 0.0f, 
         0.0f, 0.0f, 1.0f, 1.0f }; 
    CGFloat locations[] = { 0.0, 1.0 }; 

    CGGradientRef myGradient = CGGradientCreateWithColorComponents(rgbColorSpace, colorSpace, locations, 2); 

    CGPoint s = CGPointMake(0, 0); 
    CGPoint e = CGPointMake(100, 100); 
    CGContextDrawLinearGradient(cx, myGradient, s, e, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation); 

    CGColorSpaceRelease(rgbColorSpace); 
    CGGradientRelease(myGradient); 
} 

這怎麼看起來像:

enter image description here

是否有可能以 「補」 了整個橢圓形?我玩BlendModes,但沒有幫助。

感謝

回答

1

嘗試用下面的更換點[]初始化代碼...

CGPoint points[] = {{0.0,10.0},{100.0,10.0},{90.0,100.0},{10.0,100.0}}; 

CoreGraphics中使用非零匝數規則來確定如何填充路徑。由於橢圓是順時針繪製的,而您的梯形是逆時針繪製的,所以重疊區域未被填充。將梯形的繪製順序更改爲順時針將導致完全填充的對象。