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

How to do custom view shapes using swift UIView?

如何在 UIView 中显示 5 点星形或多个星形?

通过使用 CGRect 获得正方形或矩形视图,通过为其添加圆角半径获得圆形视图。

如何获取星形UIView

你应该看看这个帖子:

在那里你可以找到一个 UIBezierpath,它画了一个星星。 然后,您可以使用此星形路径并将其用作 CAShapeLayer 的路径。

然后你必须将这个 CAShapeLayer 添加到你的 UIView 层中。

这是代码

    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