Skip to content

Instantly share code, notes, and snippets.

@inamiy
Last active August 19, 2021 10:18
Show Gist options
  • Save inamiy/d64d4bc52cdc013439b4e57078b4d223 to your computer and use it in GitHub Desktop.
Save inamiy/d64d4bc52cdc013439b4e57078b4d223 to your computer and use it in GitHub Desktop.
import SwiftUI
extension View {
func pipe<R>(@ViewBuilder _ f: (Self) -> R) -> R {
f(self)
}
}
struct ContentView: View {
@State var flag: Bool = false
var body: some View {
Rectangle()
// 1. ternary conditional operator
// .foregroundColor(flag ? .red : .blue)
// 2. pipe
.pipe {
if flag {
$0.foregroundColor(.red)
} else {
$0.foregroundColor(.blue)
}
}
.onTapGesture {
withAnimation {
flag.toggle()
}
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment