2012-04-06 3 views
8

CAShapeLayer에서 나는 UIBezierPath을 닫았습니다. fillColor을 설정하여이 도형을 채울 수 있지만 그래디언트로 도형을 채우고 싶습니다. CAGradientLayer을 설정하여 베 지어 경로로 윤곽선이 표시된 모양으로자를 수 있습니까?iOS에서 그라데이션을 사용하여 경로 채우기

답변

5

원하는 것은 마스크입니다.

[gradientLayer setMask:shapeLayer];

19

이 초안의 예는 다음과 같습니다 : CAGradientlayer 지금처럼 모양의 경계에 클립 수있는 -setMask 방법이

... 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
CGContextRef context = UIGraphicsGetCurrentContext(); 

UIColor *gradientColor = [UIColor colorWithRed:0.51 green:0.0 blue:0.49 alpha:1.0]; 

NSArray *gradientColors = [NSArray arrayWithObjects: 
          (id)[UIColor blueColor].CGColor, 
          (id)gradientColor.CGColor, 
          (id)[UIColor redColor].CGColor, nil]; 
CGFloat gradientLocations[] = {0, 0.5, 1}; 
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)gradientColors, gradientLocations); 

UIBezierPath *roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 200, 200) cornerRadius:6]; 
CGContextSaveGState(context); 
[roundedRectanglePath fill]; 
[roundedRectanglePath addClip]; 
CGContextDrawLinearGradient(context, gradient, CGPointMake(10, 10), CGPointMake(210, 10), 0); 
CGColorSpaceRelease(colorSpace); 
CGGradientRelease(gradient); 

... 
+0

이것은 좋은입니다,하지만 당신은해야 그라데이션 및 색상 공간을 해제합니다. – Sulthan

+0

덕분에, 나는 원래의 게시물이 마스크 기능을하기 때문에 그들에게 – WhiteTiger

+0

을 넣어 잊었, 나는 또한 추가 한 다음 'CGContextAddPath (문맥, roundedRectanglePath.CGPath)' 'CGContextClip (컨텍스트)' CGContextDrawLinearGradient''전 – maggix

관련 문제