-
-
Save chriseidhof/1f340e588de9b657dd3f452d13f9a583 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() | |
.frame(width: flag ? 300 : 200) | |
// 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