如何使用Swift UIView自定义视图形状?

时间:2019-06-27 09:04:52

标签: swift uiview

如何显示星形UI查看5点星还是很多点?

使用CGRect可获得正方形或矩形形状的视图,通过向其添加拐角半径获得圆形形状的视图。

如何获取星形UIView

2 个答案:

答案 0 :(得分:0)

您应该看一下这个线程:How to make star shape with bezier path in ios?

您可以在其中找到绘制星星的UIBezierpath。 然后,您可以使用此星形路径并将其用作CAShapeLayer的路径。

然后,您必须将此CAShapeLayer添加到UIView的层中。

答案 1 :(得分:0)

这是代码

    let starPath = UIBezierPath(rect: CGRect(x: 30, y: 30, width: 50, height: 50))
    starPath.move(to: CGPoint(x: 45.25, y: 0))
    starPath.addLine(to: CGPoint(x: 61.13, y: 23))
    starPath.addLine(to: CGPoint(x: 88.29, y: 30.75))
    starPath.addLine(to: CGPoint(x: 70.95, y: 52.71))
    starPath.addLine(to: CGPoint(x: 71.85, y: 80.5))
    starPath.addLine(to: CGPoint(x: 45.25, y: 71.07))
    starPath.addLine(to: CGPoint(x: 18.65, y: 80.05))
    starPath.addLine(to: CGPoint(x: 19.55, y: 52.71))
    starPath.addLine(to: CGPoint(x: 2.21, y: 30.75))
    starPath.addLine(to: CGPoint(x: 29.37, y: 23))
    starPath.close()
    starPath.lineWidth = 1
    UIColor.red.setStroke()
    starPath.stroke()

    let shapeLayer = CAShapeLayer()
    shapeLayer.path = starPath.cgPath