UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Frilly circle border ?

Forums > SwiftUI

@kwolk  

Is there a way to create a frilly border for a circle like this:

2      

import SwiftUI

struct WaveShape: Shape {
//    let radius : CGFloat = 50 // derived from the rectangle we're inside of
    let offset : CGFloat = 15
    let steps : Int = 20

    func path(in rect: CGRect) -> Path {
        let center = CGPoint(x: rect.midX, y: rect.midY)
        let radius = min(rect.size.height, rect.size.width) / 2
        let anglesubdiv = CGFloat(0.25) * CGFloat(360/steps) * CGFloat.pi / 180.0

        var path = Path()
        path.move(to: CGPoint(x: rect.maxX, y: rect.midY)) // 3 o'clock

        for i in (0..<steps) {
            let angle = CGFloat(i) * CGFloat(360/steps) * CGFloat.pi / 180.0

            let p1 = CGPoint(
                x: center.x + (radius+offset) * cos(angle+anglesubdiv),
                y: center.y + (radius+offset) * sin(angle+anglesubdiv)
            )
            let p2 = CGPoint(
                x: center.x + (radius) * cos(angle+anglesubdiv*2.0),
                y: center.y + (radius) * sin(angle+anglesubdiv*2.0)
            )
            let p3 = CGPoint(
                x: center.x + (radius-offset) * cos(angle+anglesubdiv*3.0),
                y: center.y + (radius-offset) * sin(angle+anglesubdiv*3.0)
            )
            let p4 = CGPoint(
                x: center.x + radius * cos(angle+anglesubdiv*4.0),
                y: center.y + radius * sin(angle+anglesubdiv*4.0)
            )
            // debug
//            path.addLine(to: p1)
//            path.addLine(to: p2)
//            path.addLine(to: p3)
//            path.addLine(to: p4)

            path.addQuadCurve(to: p2, control: p1)
            path.addQuadCurve(to: p4, control: p3)

        }

        path.closeSubpath()

        return path
    }
}

struct ContentView: View {
    var body: some View {
        VStack {
            WaveShape()
                .stroke(.black, lineWidth: 2)
        }
        .border(.red)

        .padding()
    }
}

#Preview {
    ContentView()
        .frame(width: 300, height: 300)
        .padding()
}

2      

@kwolk  

Sorry for the late reply !

Many thanks for your help !

I actually found following Paul's Spirograph tutorial actually got me there: watch?v=V2fxC92HGnQ

However, it was very good of you to contribute !

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.