2013-07-17 5 views
0

나는이 사용하는 석영과 같은 몇 가지 효과를 할 싶습니다Quartz를 사용하여 스포트 라이트 효과를 얻는 방법?

enter image description here

나는 이미지 뷰의 상단에 레이어를 가지고 싶어하지 않을 것이다, 그러나이 지역의 일부는 더 내가 그것을 구현할 수있는 방법 덮여있다 ? 어떤 아이디어? 감사.

+0

이 오픈 소스 프로젝트를 살펴보십시오. https://www.cocoacontrols.com/controls/emhint – Groot

답변

1

iOS 또는 MacOS에 대해 이야기하고 있는지 여부는 분명하지 않지만 교장은 동일합니다.

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(ctx); 

    // Replace with your location, spot radius, and blur radius 
    CGPoint spotLoc = CGPointMake(200,200); 
    CGFloat spotRadius = 100; 
    CGFloat blurRadius = 5; 

    // Draw 50% black overlay over the entire view 
    CGContextSetFillColorWithColor(ctx, [[UIColor colorWithRed: 0 green:0 blue:0 alpha:0.5] CGColor]); 
    CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx)); 

    // Then punch out spot using an offset shadow 
    CGRect spotRect = CGRectMake(spotLoc.x - spotRadius, spotLoc.y - spotRadius, 2 * spotRadius, 2 * spotRadius); 
    CGFloat xOffset = CGRectGetMaxX(spotRect) - CGRectGetMinX(self.bounds); 
    CGContextSetBlendMode(ctx, kCGBlendModeDestinationOut); 
    CGContextSetFillColorWithColor(ctx, [[UIColor blackColor] CGColor]); 
    CGContextSetShadowWithColor(ctx, CGSizeMake(xOffset, 0), blurRadius, [[UIColor blackColor] CGColor]); 
    CGContextTranslateCTM(ctx, -xOffset, 0); 
    CGContextFillEllipseInRect(ctx, spotRect); 

    CGContextRestoreGState(ctx); 
} 

당신이, 돈 하드 가장자리를 원하는 경우 : 내가 생각할 수있는 가장 쉬운 방법은 반투명 회색 색상 오버레이 뷰를 작성하고 다음 오프셋 그림자와 펀치 아웃하는 것입니다 ... 여기에 간단한 예제입니다 오프셋 및 그림자가있는 경우에도 블렌드 모드를 kCGBlendModeDestinationOut으로 설정 한 후 불투명 한 채우기로 CGContextFillEllipseInRect을 사용하면됩니다.

EDIT : 방사형 그래디언트 효과와 비슷한 작업을 수행 할 수도 있지만 예를 들어 적용한 효과를 보면 가장자리가 더 흐리게 보이고 균일 한 그래디언트가 아니라는 것을 알 수 있습니다. 말하기가 어렵습니다. 어쨌든 Quartz는 섀도 잉 메커니즘을 사용하여 흐림 효과를 줄 수 있으므로 사용하지 않는 것이 좋습니다.

관련 문제