如何在ios中使用bezier路径制作星形?

时间:2016-07-13 05:41:38

标签: ios objective-c swift drawing uibezierpath

我正在使用 Bezier路径并使用它制作简单的形状。使用Bezier路径制作星形的过程是什么?

2 个答案:

答案 0 :(得分:2)

尝试此路径:

//// Star Drawing
UIBezierPath* starPath = [UIBezierPath bezierPath];
[starPath moveToPoint: CGPointMake(45.25, 0)];
[starPath addLineToPoint: CGPointMake(61.13, 23)];
[starPath addLineToPoint: CGPointMake(88.29, 30.75)];
[starPath addLineToPoint: CGPointMake(70.95, 52.71)];
[starPath addLineToPoint: CGPointMake(71.85, 80.5)];
[starPath addLineToPoint: CGPointMake(45.25, 71.07)];
[starPath addLineToPoint: CGPointMake(18.65, 80.5)];
[starPath addLineToPoint: CGPointMake(19.55, 52.71)];
[starPath addLineToPoint: CGPointMake(2.21, 30.75)];
[starPath addLineToPoint: CGPointMake(29.37, 23)];
[starPath closePath];
[UIColor.redColor setStroke];
starPath.lineWidth = 1;
[starPath stroke];

您可以使用Paint Code app为iOS和OSX绘制形状。 这是一个易于使用且非常有用的应用程序。

快乐编码。

答案 1 :(得分:1)

我认为这就是你要找的东西:

func starPathInRect(rect: CGRect) -> UIBezierPath {
    let path = UIBezierPath()

    let starExtrusion:CGFloat = 30.0

    let center = CGPointMake(rect.width / 2.0, rect.height / 2.0)

    let pointsOnStar = 5 + arc4random() % 10

    var angle:CGFloat = -CGFloat(M_PI / 2.0)
    let angleIncrement = CGFloat(M_PI * 2.0 / Double(pointsOnStar))
    let radius = rect.width / 2.0

    var firstPoint = true

    for i in 1...pointsOnStar {

        let point = pointFrom(angle, radius: radius, offset: center)
        let nextPoint = pointFrom(angle + angleIncrement, radius: radius, offset: center)
        let midPoint = pointFrom(angle + angleIncrement / 2.0, radius: starExtrusion, offset: center)

        if firstPoint {
            firstPoint = false
            path.moveToPoint(point)
        }

        path.addLineToPoint(midPoint)
        path.addLineToPoint(nextPoint)

        angle += angleIncrement
    }

    path.closePath()

    return path
}
  

此功能可让您决定星球中您想要多少点。

点击link查看更多detailed description

希望这有助于。 :)让我知道任何查询.. !!!

相关问题