我想要我的
ImageView的Hex Hexagon形状.但是在实现下面的代码之后,我得到这个图像
- (UIBezierPath *)roundedpolygonPathWithRect:(CGRect)square
linewidth:(CGFloat)linewidth
sides:(NSInteger)sides
cornerRadius:(CGFloat)cornerRadius
{
UIBezierPath *path = [UIBezierPath bezierPath];
CGFloat theta = 2.0 * M_PI / sides; // how much to turn at every corner
CGFloat offset = cornerRadius * tanf(theta / 2.0); // offset from which to start rounding corners
CGFloat squareWidth = MIN(square.size.width,square.size.height); // width of the square
// calculate the length of the sides of the polygon
CGFloat length = squareWidth - linewidth;
if (sides % 4 != 0) { // if not dealing with polygon which will be square with all sides ...
length = length * cosf(theta / 2.0) + offset/2.0; // ... offset it inside a circle inside the square
}
CGFloat sideLength = length * tanf(theta / 2.0);
// start drawing at `point` in lower right corner
CGFloat calc = squareWidth / 2.0 + sideLength / 2.0 - offset;
CGPoint point = CGPointMake(calc,squareWidth - (squareWidth - length) / 2.0);
CGFloat angle = M_PI;
[path movetoPoint:point];
// draw the sides and rounded corners of the polygon
for (NSInteger side = 0; side < sides; side++)
{
point = CGPointMake(point.x + (sideLength - offset * 2.0) * cosf(angle),point.y + (sideLength - offset * 2.0) * sinf(angle));
[path addLinetoPoint:point];
CGPoint center = CGPointMake(point.x + cornerRadius * cosf(angle + M_PI_2),point.y + cornerRadius * sinf(angle + M_PI_2));
[path addArcWithCenter:center radius:cornerRadius startAngle:angle - M_PI_2 endAngle:angle + theta - M_PI_2 clockwise:YES];
point = path.currentPoint; // we don't have to calculate where the arc ended ... UIBezierPath did that for us
angle += theta;
}
[path closePath];
return path;
}
CGFloat linewidth = 5.0;
UIBezierPath *path = [self roundedpolygonPathWithRect:cell.eventimageView.bounds
linewidth:linewidth
sides:6
cornerRadius:10];
CAShapeLayer *mask = [CAShapeLayer layer];
mask.path = path.CGPath;
mask.linewidth = linewidth;
mask.strokeColor = [UIColor clearColor].CGColor;
mask.fillColor = [UIColor whiteColor].CGColor;
cell.eventimageView.layer.mask = mask;
CAShapeLayer *border = [CAShapeLayer layer];
border.path = path.CGPath;
border.linewidth = linewidth;
border.strokeColor = [UIColor blackColor].CGColor;
border.fillColor = [UIColor clearColor].CGColor;
[cell.eventimageView.layer addSublayer:border];
请帮助我如何实现这一点,我从来没有使用过Bezier Paths.
提前致谢 !
解决方法
我重写Swift中的路径创建功能,也可以使用rotateOffset参数,允许任意旋转正则多边形.
我不完全确定我的功能与你所使用的功能相同(因为我使用极坐标绘制多边形),而是产生的结果与你想要的相似.
public func roundedpolygonPath(rect: CGRect,linewidth: CGFloat,sides: NSInteger,cornerRadius: CGFloat,rotationOffset: CGFloat = 0) -> UIBezierPath {
let path = UIBezierPath()
let theta: CGFloat = CGFloat(2.0 * M_PI) / CGFloat(sides) // How much to turn at every corner
let offset: CGFloat = cornerRadius * tan(theta / 2.0) // Offset from which to start rounding corners
let width = min(rect.size.width,rect.size.height) // Width of the square
let center = CGPoint(x: rect.origin.x + width / 2.0,y: rect.origin.y + width / 2.0)
// Radius of the circle that encircles the polygon
// Notice that the radius is adjusted for the corners,that way the largest outer
// dimension of the resulting shape is always exactly the width - linewidth
let radius = (width - linewidth + cornerRadius - (cos(theta) * cornerRadius)) / 2.0
// Start drawing at a point,which by default is at the right hand edge
// but can be offset
var angle = CGFloat(rotationOffset)
let corner = CGPointMake(center.x + (radius - cornerRadius) * cos(angle),center.y + (radius - cornerRadius) * sin(angle))
path.movetoPoint(CGPointMake(corner.x + cornerRadius * cos(angle + theta),corner.y + cornerRadius * sin(angle + theta)))
for _ in 0..<sides {
angle += theta
let corner = CGPointMake(center.x + (radius - cornerRadius) * cos(angle),center.y + (radius - cornerRadius) * sin(angle))
let tip = CGPointMake(center.x + radius * cos(angle),center.y + radius * sin(angle))
let start = CGPointMake(corner.x + cornerRadius * cos(angle - theta),corner.y + cornerRadius * sin(angle - theta))
let end = CGPointMake(corner.x + cornerRadius * cos(angle + theta),corner.y + cornerRadius * sin(angle + theta))
path.addLinetoPoint(start)
path.addQuadCurvetoPoint(end,controlPoint: tip)
}
path.closePath()
// Move the path to the correct origins
let bounds = path.bounds
let transform = CGAffineTransformMakeTranslation(-bounds.origin.x + rect.origin.x + linewidth / 2.0,-bounds.origin.y + rect.origin.y + linewidth / 2.0)
path.applyTransform(transform)
return path
}
例如,将rotationOffset设置为M_PI / 6.0,生成的形状将如下所示
以防万一,你可以看到我使用的完整的操场here