【问题标题】:How to make star shape with bezier path in ios?如何在ios中使用贝塞尔路径制作星形?
【发布时间】:2016-07-13 05:41:37
【问题描述】:

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

【问题讨论】:

标签: ios objective-c swift drawing uibezierpath


【解决方案1】:

试试这条路:

//// 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 应用程序为 iOS 和 OSX 绘制形状。 这是易于使用且非常有用的应用程序。

快乐编码。

【讨论】:

  • 你能不能把它弄大一点。
【解决方案2】:

我想这就是你要找的:

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

希望这会有所帮助。 :) 如有任何疑问,请告诉我..!!!

【讨论】:

    【解决方案3】:

    我升级了@Sneha 的算法。 你可以这样使用它:

    rect = CGRect(x: 0, y: 0, width: 15, height: 15)
    path.addStar(rect: rect, extrusion: 20, points: 5) 
    path.close()
    

    这是算法:

    extension CGPoint {
        func pointFrom(angle: CGFloat, radius: CGFloat) -> CGPoint {
            return CGPoint(x: self.x + radius * cos(CGFloat.pi - angle), y: self.y - radius * sin(CGFloat.pi - angle))
        }
    }
    
    extension UIBezierPath {
        func addStar(rect: CGRect, extrusion: CGFloat, points: Int) {
            self.move(to: CGPoint(x: 0, y: 0))
            let center = CGPoint(x: rect.width / 2.0, y: rect.height / 2.0)
            var angle:CGFloat = -CGFloat.pi / 2.0
            let angleIncrement = CGFloat.pi * 2.0 / CGFloat(points)
            let radius = rect.width / 2.0
            var firstPoint = true
            for _ in 1...points {
                let point = center.pointFrom(angle: angle, radius: radius)
                let nextPoint = center.pointFrom(angle: angle + angleIncrement, radius: radius)
                let midPoint = center.pointFrom(angle: angle + angleIncrement / 2.0, radius: extrusion)
                if firstPoint {
                    firstPoint = false
                    self.move(to: point)
                }
                self.addLine(to: midPoint)
                self.addLine(to: nextPoint)
                angle += angleIncrement
                }
        }
    

    【讨论】:

    • 我认为作为便利初始化器会好得多
    • 也许 :) .... .
    猜你喜欢
    • 1970-01-01
    • 2015-02-23
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 2015-04-19
    相关资源
    最近更新 更多