Last active
August 19, 2021 10:18
-
-
Save inamiy/d64d4bc52cdc013439b4e57078b4d223 to your computer and use it in GitHub Desktop.
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
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