Last active
May 2, 2024 11:44
-
-
Save thilojaeggi/bace7d361637a66ead38c0349e5ddefa to your computer and use it in GitHub Desktop.
Swift uneven Rounded Rectangle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UnevenRoundedRectangle.swift | |
// | |
// Created by Thilo on 10.08.2023. | |
// | |
import Foundation | |
import SwiftUI | |
struct RectangleCornerRadii { | |
var topLeading: CGFloat = 0 | |
var topTrailing: CGFloat = 0 | |
var bottomLeading: CGFloat = 0 | |
var bottomTrailing: CGFloat = 0 | |
} | |
struct UnevenRoundedRectangle: Shape { | |
var cornerRadii: RectangleCornerRadii | |
func path(in rect: CGRect) -> Path { | |
var path = Path() | |
let width = rect.size.width | |
let height = rect.size.height | |
// Start at top leading corner | |
path.move(to: CGPoint(x: cornerRadii.topLeading, y: 0)) | |
// Top line to top trailing corner | |
path.addLine(to: CGPoint(x: width - cornerRadii.topTrailing, y: 0)) | |
path.addArc(center: CGPoint(x: width - cornerRadii.topTrailing, y: cornerRadii.topTrailing), radius: cornerRadii.topTrailing, startAngle: .degrees(-90), endAngle: .degrees(0), clockwise: false) | |
// Right side line to bottom trailing corner | |
path.addLine(to: CGPoint(x: width, y: height - cornerRadii.bottomTrailing)) | |
path.addArc(center: CGPoint(x: width - cornerRadii.bottomTrailing, y: height - cornerRadii.bottomTrailing), radius: cornerRadii.bottomTrailing, startAngle: .degrees(0), endAngle: .degrees(90), clockwise: false) | |
// Bottom line to bottom leading corner | |
path.addLine(to: CGPoint(x: cornerRadii.bottomLeading, y: height)) | |
path.addArc(center: CGPoint(x: cornerRadii.bottomLeading, y: height - cornerRadii.bottomLeading), radius: cornerRadii.bottomLeading, startAngle: .degrees(90), endAngle: .degrees(180), clockwise: false) | |
// Left side line to top leading corner | |
path.addLine(to: CGPoint(x: 0, y: cornerRadii.topLeading)) | |
path.addArc(center: CGPoint(x: cornerRadii.topLeading, y: cornerRadii.topLeading), radius: cornerRadii.topLeading, startAngle: .degrees(180), endAngle: .degrees(270), clockwise: false) | |
return path | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment