How to make star shape with bezier path in ios?

烂漫一生 提交于 2020-01-07 09:23:17

问题


I am working with Bezier path and making simple shapes with it. What is the process for making a star shape using Bezier paths?


回答1:


Try this path:

//// 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];

You can use Paint Code app to draw shape for iOS and OSX. This is easy to use and very helpful app.

Happy Coding.




回答2:


I think this is what you are looking for:

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
}

This Function let you decide How many points you want in your stars..

Check this link for more detailed description.

Hope this Helps. :) Let me know for any query..!!!



来源:https://stackoverflow.com/questions/38343458/how-to-make-star-shape-with-bezier-path-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!