2014-06-15 5 views
2

UIBezierPath 및 ZEPolygon을 사용하여 육각형을 그리려는데, 잘 작동하지만 육각면이 평평합니다. 나는 일을하는 경로에 180도 변형을 포함하여 중간에 점을 그리는 데 모든 것을 시도했지만 그 밖의 모든 것은 어지럽 힙니다. 당신이 CGTransf와 UIBezierPath을 회전 할 때UIBezierPath (첫 번째 점)를 사용하여 육각형 그리기

This is how it looks now

This is how i would like it to look

내 코드는 어떤 도움

+1

180도 변환을에서 다음과 같이해야해야 하는가? 90을 원하지 않니? –

+0

죄송합니다. 90도는 정확하지만 오프셋은 모두 정확하지 않으며 밑의 이미지는 보이지 않습니다. –

답변

1

에 대한

UIImageView *maskedImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]]; 

UIBezierPath *nonagon = [UIBezierPath bezierPathWithPolygonInRect:maskedImageView.frame numberOfSides:6]; 

CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 
shapeLayer.path = nonagon.CGPath; 

maskedImageView.layer.mask = shapeLayer; 

[self.view addSubview:maskedImageView]; 

This is the library i used for the drawing the bezier path

감사합니다 아래 orm은 점 (0,0)을 중심으로 회전하고, 경로의 경우 점 (0,0)은 도형의 왼쪽 위 모서리입니다. 이것은 잘못된 점을 중심으로 회전하는 다른 작업을 수행하는 경우에만 90도 회전 할 때 오프셋이 올바르지 않은 이유입니다.

회전하기 전에 경로의 중심을 점 (0,0)으로 맞추고 회전 한 다음 (0,0)이 맨 위 왼쪽에 있도록 다시 이동해야합니다.

다음 코드는 폴리곤으로 90도 회전합니다

// get the size of the image, we'll need this for our path and for later too 
CGRect boundsForPoly = maskedImageView.frame; 
// create our path inside the rect 
UIBezierPath *nonagon = [UIBezierPath bezierPathWithPolygonInRect:boundsForPoly numberOfSides:6]; 
// center the polygon on (0,0) 
[nonagon applyTransform:CGAffineTransformMakeTranslation(-boundsForPoly.size.width/2, -boundsForPoly.size.height/2)]; 
// rotate it 90 degrees 
[nonagon applyTransform:CGAffineTransformMakeRotation(M_PI/2)]; 
// now move it back so that the top left of its bounding box is (0,0) 
[nonagon applyTransform:CGAffineTransformMakeTranslation(nonagon.bounds.size.width/2, nonagon.bounds.size.height/2)]; 

이 다각형으로 90도 회전되며, 파란색 개요는 (0,0)

에서이 왼쪽 상단의 유지 당신의 adam.wulf 전에서 대답에 문제가

enter image description here

+0

환상적입니다. 정말 고맙습니다 –

0

있습니다 : 전에 경로 및 녹색 윤곽이 회전 이후 광고들과

[구 각형 applyTransform : CGAffineTransformMakeTranslation (nonagon.bounds.size.width/2 nonagon.bounds.size.height/2)];

프레임의 중심에 다각형의 중심을 맞추지 않습니다. 대신

//Centered version 
[nonagon applyTransform:CGAffineTransformMakeTranslation(boundsForPoly.size.width/2, boundsForPoly.size.height/2/2)]; 

는 따라서 코드가 adam.wulf

// get the size of the image, we'll need this for our path and for later too 
CGRect boundsForPoly = maskedImageView.frame; 
// create our path inside the rect 
UIBezierPath *nonagon = [UIBezierPath bezierPathWithPolygonInRect:boundsForPoly numberOfSides:6]; 
// center the polygon on (0,0) 
[nonagon applyTransform:CGAffineTransformMakeTranslation(-boundsForPoly.size.width/2, -boundsForPoly.size.height/2)]; 
// rotate it 90 degrees 
[nonagon applyTransform:CGAffineTransformMakeRotation(M_PI/2)]; 
// now move it back so that the top left of its bounding box is (0,0) 
[nonagon applyTransform:CGAffineTransformMakeTranslation(boundsForPoly.size.width/2, boundsForPoly.size.height/2/2)]; 
관련 문제