2010-01-18 61 views
2

我有一個簡單的圓形在我的子分類uiview如下所示。我如何在circe的底部添加輕微的眩暈?iphone卓爾圓陰影

- (void)drawRect:(CGRect)rect 
    { 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    UIGraphicsPushContext(ctx); 
    CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f); // white color 
    CGContextFillEllipseInRect(ctx, CGRectMake(10.0f, 10.0f, 100.0f, 100.0f)); // a white filled circle with a diameter of 100 pixels, centered in (60, 60) 
    UIGraphicsPopContext(); 

    //Now what here? 
    } 

回答

4

要遵循SLF的答案,你會替換代碼上面:

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    UIGraphicsPushContext(ctx); 
    CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f); // white color 
    CGContextSetShadow(ctx, CGSizeMake(2.0f, 2.0f), 2.0f); 
    CGContextFillEllipseInRect(ctx, CGRectMake(10.0f, 10.0f, 100.0f, 100.0f)); // a white filled circle with a diameter of 100 pixels, centered in (60, 60) 
    UIGraphicsPopContext(); 
} 

這將創建一個由2個像素向下偏移和你的圈子右側的陰影中, 2像素的模糊。您可以更改這些值以創建所需的效果。 CGContextSetShadowWithColor()也可以使用與黑色不同的顏色,如果您想爲該圖形添加發光效果。

1

請參閱Quartz2D Shadows指南。

CGContextSetShadowWithColor (myContext, myShadowOffset, 5, myColor); 
+0

我們如何設置陰影或不透明度的大小? – 2013-12-20 02:46:16

+1

@VanDuTran上面的'5'是尺寸,但他們稱之爲模糊。不透明度是顏色的一部分。 – slf 2013-12-20 14:39:07

+1

嗯..好吧,這很奇怪,他們如何使用這個.. 4和10的模糊非常輕,而6到8的值更「不透明」。我把它設置爲20,根本看不到陰影 – 2013-12-20 15:35:37