Created
October 13, 2020 21:01
-
-
Save dfrobison/c0fd76a6e47cef87787b45fab28cf4ef to your computer and use it in GitHub Desktop.
[Move onChange into binding]
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 | |
struct ContentView: View { | |
@State private var rating = 0.0 | |
var body: some View { | |
Slider(value: $rating.onChange(sliderChanged)) | |
} | |
func sliderChanged(_ value: Double) { | |
print("Rating changed to \(value)") | |
} | |
} | |
extension Binding { | |
func onChange(_ handler: @escaping (Value) -> Void) -> Binding<Value> { | |
Binding( | |
get: { self.wrappedValue }, | |
set: { newValue in | |
self.wrappedValue = newValue | |
handler(newValue) | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment