Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 3:六边形不显示图像视图的中心_Ios_Swift_Xcode - Fatal编程技术网

Ios Swift 3:六边形不显示图像视图的中心

Ios Swift 3:六边形不显示图像视图的中心,ios,swift,xcode,Ios,Swift,Xcode,我的六边形不显示在图像视图的中心,如何修复此问题?这是我的密码 func roundedPolygonPath(rect: CGRect, lineWidth: CGFloat, sides: NSInteger, cornerRadius: CGFloat, rotationOffset: CGFloat = 0) -> UIBezierPath { let path = UIBezierPath() let theta: CGFloat

我的六边形不显示在图像视图的中心,如何修复此问题?这是我的密码

 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 + 10 / 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 = CGPoint(center.x + (radius - cornerRadius) * cos(angle), center.y + (radius - cornerRadius) * sin(angle))
        path.move(to: CGPoint(corner.x + cornerRadius * cos(angle + theta), corner.y + cornerRadius * sin(angle + theta)))

        for _ in 0 ..< sides {
            angle += theta

            let corner = CGPoint(center.x + (radius - cornerRadius) * cos(angle), center.y + (radius - cornerRadius) * sin(angle))
            let tip = CGPoint(center.x + radius * cos(angle), center.y + radius * sin(angle))
            let start = CGPoint(corner.x + cornerRadius * cos(angle - theta), corner.y + cornerRadius * sin(angle - theta))
            let end = CGPoint(corner.x + cornerRadius * cos(angle + theta), corner.y + cornerRadius * sin(angle + theta))

            path.addLine(to: start)
            path.addQuadCurve(to: end, controlPoint: tip)
        }

        path.close()

        // Move the path to the correct origins
        let bounds = path.bounds
        let transform = CGAffineTransform(translationX: -bounds.origin.x + rect.origin.x + lineWidth / 2.0,
                                          y: -bounds.origin.y + rect.origin.y + lineWidth / 2.0)
        path.apply(transform)

        return path
}
func roundedPolygonPath(rect:CGRect,线宽:CGFloat,边:NSInteger,角半径:CGFloat,旋转偏移:CGFloat=0)
->尤比齐尔路径{
let path=UIBezierPath()
让θ:CGFloat=CGFloat(2.0*M_PI)/CGFloat(边)//在每个拐角处转弯多少
//let offset:CGFloat=cornerRadius*tan(θ/2.0)//开始圆角的偏移量
设width=min(rect.size.width,rect.size.height)//正方形的宽度
设中心=CGPoint(x:rect.origin.x+width+10/2.0,y:rect.origin.y+width/2.0)
//包围多边形的圆的半径
//请注意,半径是为角点调整的,这样最大的外部
//结果形状的尺寸始终正好是宽度-线宽
设半径=(宽度-线宽+拐角半径-(cos(θ)*拐角半径))/2.0
//从一个点开始绘图,默认情况下,该点位于右侧边缘
//但是可以抵消
变量角度=CGFloat(旋转偏移)
设角点=CGPoint(中心x+(半径-角点半径)*cos(角度),中心y+(半径-角点半径)*sin(角度))
移动路径(到:CGPoint(角点.x+角点半径*cos(角度+θ),角点.y+角点半径*sin(角度+θ)))
对于0..<边中的uu{
角度+=θ
设角点=CGPoint(中心x+(半径-角点半径)*cos(角度),中心y+(半径-角点半径)*sin(角度))
让尖端=CGPoint(中心x+半径*cos(角度),中心y+半径*sin(角度))
让起点=CGPoint(角点x+角点半径*cos(角-θ),角点y+角点半径*sin(角-θ))
设端点=CGPoint(角点x+角点半径*cos(角+θ),角点y+角点半径*sin(角+θ))
path.addLine(to:start)
addQuadCurve(到:结束,控制点:尖端)
}
path.close()
//将路径移动到正确的原点
设bounds=path.bounds
让transform=CGAffineTransform(translationX:-bounds.origin.x+rect.origin.x+lineWidth/2.0,
y:-边界.origin.y+矩形origin.y+线宽/2.0)
路径。应用(转换)
返回路径
}

这两个元素都有
x
y
等于
0
。尝试使用
width
属性计算变换。

正六边形的宽度和高度不相等<代码>高度=a;宽度=a*sqrt(3)/2在您的情况下;因此,您需要通过
a*(1-(sqrt(3)/2))/2
或其他方式来转换点。您能建议我在代码中做什么吗?您需要在每个
x
坐标上应用偏移量,它将您的六边形对齐到您视图中所需的位置。@MianShahbazAkram,我已经告诉过您该怎么做,更改
let transform=CGAffineTransform…
以使用
width
height
属性,而不是
x
y