Skip to content

Instantly share code, notes, and snippets.

@Koshimizu-Takehito
Last active August 4, 2024 03:14
Show Gist options
  • Save Koshimizu-Takehito/0695697bf80e16b02f044f0865b941f8 to your computer and use it in GitHub Desktop.
Save Koshimizu-Takehito/0695697bf80e16b02f044f0865b941f8 to your computer and use it in GitHub Desktop.
UnevenRoundedRectangle Sample 2
import SwiftUI
struct ContentView: View {
@ViewBuilder
private func leftHandSide() -> some View {
HelloWorld()
.clipShape(Rectangle())
HelloWorld()
.clipShape(RoundedRectangle(cornerRadius: 20))
HelloWorld()
.clipShape(UnevenRoundedRectangle(topLeadingRadius: 80))
}
@ViewBuilder
private func rightHandSide() -> some View {
HelloWorld()
.clipShape(.rect)
HelloWorld()
.clipShape(.rect(cornerRadius: 20))
HelloWorld()
.clipShape(.rect(topLeadingRadius: 80))
}
var body: some View {
HStack(spacing: 20) {
VStack(spacing: 20, content: leftHandSide)
.environment(MyStyle(background: .orange))
VStack(spacing: 20, content: rightHandSide)
.environment(MyStyle(background: .mint))
}
.padding(20)
}
}
struct HelloWorld: View {
@Environment(MyStyle.self) var state
var body: some View {
Text("Hello, world!")
.font(.largeTitle.bold())
.foregroundStyle(state.foreground)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(state.background.gradient)
}
}
@Observable
class MyStyle {
let foreground: Color
let background: Color
init(foreground: Color = .white, background: Color = .black) {
self.foreground = foreground
self.background = background
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment