Alex Does Things Poorly

Alex does a variety of things poorly - swift, data science, datavis, ML, drawing, AR, unity, etc, etc.

26 June 2021

Swift UI Project 3, Day 2

by alex

Another day of 100 Days of SwiftUI today, basically applying the fundamentals we learned about yesterday.

My favorite part of it was that I got to use my new knowledge to make a custom Shape for .clipShape for a side project at work. I need a way to clip a view/color to a flag shape, and specifically to a [Swallowtail](https://en.wikipedia.org/wiki/Swallowtail_(flag) flag shape.

struct SwallowtailShape: Shape {
    let cutPercentage: CGFloat
    
    init(cutPercentage: CGFloat = 0.15) {
        self.cutPercentage = cutPercentage
    }
    
    func path(in rect: CGRect) -> Path {
        var path = Path()
        
        path.move(to: CGPoint(x: rect.minX, y: rect.minY))
        path.addLine(to: CGPoint(x: rect.maxX, y: rect.minY))
        path.addLine(to: CGPoint(x: rect.maxX - (rect.maxX * cutPercentage), y: rect.midY))
        path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
        path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))
        
        return path
    }
}

struct ContentView: View {
    var body: some View {
        Color.green
            .frame(width: 200, height: 100)
            .clipShape(SwallowtailShape())
        }
    }
}

And voila:

Swallowtail Flag

tags: swift - swiftui