Created
December 4, 2022 21:57
-
-
Save b3ll/bdcca0178ff410ad50cfc011c38426a3 to your computer and use it in GitHub Desktop.
Switchable Corner Shapes in SwiftUI
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
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