Skip to content

Instantly share code, notes, and snippets.

@HarshilShah
Last active April 28, 2023 04:05
Show Gist options
  • Save HarshilShah/aed21fd622448156e86a610910bc318c to your computer and use it in GitHub Desktop.
Save HarshilShah/aed21fd622448156e86a610910bc318c to your computer and use it in GitHub Desktop.
A more concise way to create a continuously rounded rectangle in SwiftUI
import SwiftUI
public struct Squircle: InsettableShape {
// MARK: Properties
var cornerSize: CGSize
var inset = 0.0
// MARK: Initializers
internal init(cornerSize: CGSize, inset: CGFloat) {
self.cornerSize = cornerSize
self.inset = inset
}
public init(cornerRadius: CGFloat) {
self.cornerSize = CGSize(width: cornerRadius, height: cornerRadius)
}
public init(cornerSize: CGSize) {
self.cornerSize = cornerSize
}
// MARK: Shape
public func path(in rect: CGRect) -> Path {
RoundedRectangle(cornerSize: cornerSize, style: .continuous)
.path(in: rect.insetBy(dx: inset, dy: inset))
}
// MARK: InsettableShape
public func inset(by amount: CGFloat) -> Squircle {
Squircle(
cornerSize: CGSize(
width: cornerSize.width - amount,
height: cornerSize.height - amount
),
inset: inset + amount
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment