我正在尝试使用 UIBezierPath 和 ZEPolygon 绘制一个六边形,效果很好,但我的六边形顶部是平的。我已经尽一切努力让它在中间绘制一个点,包括在路径上进行 180 度变换,但其他一切都中断了。
This is how it looks now
This is how i would like it to look
我的代码在下面
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
感谢您的帮助
Best Answer-推荐答案 strong>
当您使用 CGTransform 旋转 UIBezierPath 时,它将围绕点 (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)
蓝色轮廓是你之前的路径,绿色轮廓是旋转之后:
关于ios - 使用 UIBezierPath 绘制六边形(点第一),我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/24228462/
|