在iOS中创建Hexagon ImageView形状

在iOS中创建Hexagon ImageView形状,第1张

概述我想要我的 ImageView的Hex Hexagon形状.但是在实现下面的代码之后,我得到这个图像 - (UIBezierPath *)roundedPolygonPathWithRect:(CGRect)square lineWidth:(CGFloat)lineWidth 我想要我的 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

总结

以上是内存溢出为你收集整理的在iOS中创建Hexagon ImageView形状全部内容,希望文章能够帮你解决在iOS中创建Hexagon ImageView形状所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1095190.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存