2013-03-16 5 views
1

가운데에서 삼각형을 잘라 반투명의 검은 색 사각형을 그리려고합니다.QuartzCore - 다각형에 둥근 모서리 추가하기

아래 코드와 완벽하게 작동합니다. 그러나 삼각형의 모서리를 둥글게하고 싶습니다.

뇌졸중을 위해 CGContextSetLineCap을 설정하고 quartz로 내 경로를 만들고 CGContextAddArcToPoint를 사용하여 둥근 모서리를 추가하는 데 몇 가지 다른 방법을 시도했지만 아무 것도 작동하지 못했습니다.

앞서 언급했듯이 아래 코드는 삼각형 컷 아웃에 사용됩니다. 삼각형의 모서리를 어떻게 둥글게 할 수 있습니까?

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGPoint position = CGPointMake(rect.size.width/2, rect.size.height * .7); 
CGSize size = CGSizeMake(rect.size.width*.8, rect.size.height*.5); 

CGFloat firstPointX = (position.x - (size.width/2)); 
CGFloat firstPointY = (position.y - (size.height)); 
CGPoint firstPoint = CGPointMake(firstPointX, firstPointY); 

CGFloat secondPointX = position.x + (size.width/2); 
CGFloat secondPointY = position.y - (size.height); 
CGPoint secondPoint = CGPointMake(secondPointX, secondPointY); 


UIBezierPath *path = [UIBezierPath bezierPath]; 
[path moveToPoint:position]; 
[path addLineToPoint:firstPoint]; 
[path moveToPoint:firstPoint]; 
[path addLineToPoint:secondPoint]; 
[path addLineToPoint:position]; 
[path closePath]; 

CGContextAddRect(context, rect); 
CGContextAddPath(context, path.CGPath); 
CGContextEOClip(context); 

UIColor *translucentColor = [UIColor colorWithWhite:0 alpha:0.5]; 
CGContextSetFillColorWithColor(context, translucentColor.CGColor); 

CGContextFillRect(context, rect); 

UIGraphicsEndImageContext(); 

편집 : 다음은 내가 달성하려고 시도하는 것의 예입니다.

enter image description here

+0

OK - 시도했기 때문에 이전 답변이 삭제되었으며 클리핑과 함께 작동하지 않을 수 있습니다. 경로를 선을 그으면 둥근 선이 잘 작동합니다. 그리고 그것은 지금 4am입니다. 나는 내일 또 다른 시도를 할 것이다. –

+0

CGContextAddArc를 사용해 보셨습니까? – badweasel

답변

3

난 당신이 CGContextAddArc을 사용하려는 생각합니다. 필요한 경우 줄의 끝 부분까지 원의 일부분을 그립니다. 다음은 UIView를 채우는 둥근 상자를 그리는 코드입니다. 삼각형의 경우 4 대신 3 줄을 사용합니다. 상단에 2 개, 하단에 중간에 1 개. 전체 경계 당신이 당신의 방법에 그것을 통과하고 당신이 원하는대로 경계 사용하여 그릴 수 인 대신 boxRect의 물론

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGRect boxRect = self.bounds; 

float bRadius = self.cornerRadius; 
float shrink = self.strokeThickness/2; 

CGContextBeginPath(context); 

// instead of this gray you could later add options for other colors 
CGContextSetGrayFillColor(context, 0.0f, self.backgroundOpacity); 
CGContextMoveToPoint(context, CGRectGetMinX(boxRect)+shrink + bRadius, CGRectGetMinY(boxRect)+shrink); 

CGContextAddArc(context, CGRectGetMaxX(boxRect)-shrink - bRadius, CGRectGetMinY(boxRect)+shrink + bRadius, bRadius, 3 * (float)M_PI/2, 0, 0); 
CGContextAddArc(context, CGRectGetMaxX(boxRect)-shrink - bRadius, CGRectGetMaxY(boxRect)-shrink - bRadius, bRadius, 0, (float)M_PI/2, 0); 
CGContextAddArc(context, CGRectGetMinX(boxRect)+shrink + bRadius, CGRectGetMaxY(boxRect)-shrink - bRadius, bRadius, (float)M_PI/2, (float)M_PI, 0); 
CGContextAddArc(context, CGRectGetMinX(boxRect)+shrink + bRadius, CGRectGetMinY(boxRect)+shrink + bRadius, bRadius, (float)M_PI, 3 * (float)M_PI/2, 0); 

CGContextClosePath(context); 
CGContextFillPath(context); 

: 이것은 내가 가지고 roundBoxView 클래스에서입니다. 이것을 삼각형으로 만들려면 두 개가 아닌 세 개의 선만 있어야하며 시작 각도와 끝 각도를 계산하려면 계산을해야 할 수도 있습니다. 상자의 각도는 항상 90도입니다 (여기서는 킥 라디 언). 삼각형의 경우 각도를 계산하거나 삼각형에 사전 설정된 종횡비를 가져야 사전 설정 시작 및 정지 각도를 사용할 수 있습니다 . 3 개의 동일한 각도로 묘사 된 예에서는 120도 또는 M_PI/1.5 크기의 단계를 수행합니다.