2010-01-18 4 views
2

다음과 같이 하위 분류 된 uiview에 그려진 간단한 원이 있습니다. 나는 어떻게 circe의 바닥에 약간의 drowshadow를 추가합니까?iphone drow shadow on circle

- (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

어떻게 그림자 나 불투명도의 크기를 설정합니까? –

+1

@VanDuTran 위의 '5'는 크기이지만 흐림이라고 부릅니다. 불투명도는 색상의 일부입니다. – slf

+1

흠 .. 괜찮아요. 이걸 사용하는 방법이 꽤 이상합니다. 4와 10의 흐림 효과는 매우 가벼운 반면, 6에서 8의 값은 "불투명"합니다. 나는 그것을 20에 놓고 그림자를 전혀 볼 수 없었다. –