Skip to content

Instantly share code, notes, and snippets.

@b3ll
Created December 4, 2022 21:57
Show Gist options
  • Save b3ll/bdcca0178ff410ad50cfc011c38426a3 to your computer and use it in GitHub Desktop.
Save b3ll/bdcca0178ff410ad50cfc011c38426a3 to your computer and use it in GitHub Desktop.
Switchable Corner Shapes in SwiftUI
public struct AlbumArtView: View {
var useContainerRelativeShape: Bool = false
public var body: some View {
// Option 1
overlayView(for: 2.0)
// Option 2
roundedCornerShape
.strokeBorder(.white, lineWidth: 2.0 * scale)
.blendMode(.plusLighter)
}
// Option 1
private var roundedCornerShape: AnyShape {
if useContainerRelativeShape {
return AnyShape(ContainerRelativeShape())
} else {
return AnyShape(RoundedRectangle(cornerRadius: cornerRadius, style: .continuous))
}
}
// Option 2
@ViewBuilder
func overlayView(for scale: CGFloat) -> some View {
if useContainerRelativeShape {
ContainerRelativeShape()
.strokeBorder(.white, lineWidth: 2.0 * scale)
.blendMode(.plusLighter)
} else {
RoundedRectangle(cornerRadius: cornerRadius, style: .continuous)
.strokeBorder(.white, lineWidth: 2.0 * scale)
.blendMode(.plusLighter)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment