Last active
October 26, 2020 08:39
-
-
Save JanC/a51e0fdefa4f001bafc05d7ea493cf47 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 | |
import Combine | |
struct MyContentView: View { | |
@State var isPresentingModal = false | |
// comment this out | |
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode> | |
var body: some View { | |
VStack { | |
Button("Show modal") { | |
isPresentingModal = true | |
} | |
.padding() | |
} | |
.sheet(isPresented: $isPresentingModal) { | |
MyModalView(viewModel: TempViewModel()) | |
} | |
.navigationBarTitle("Details") | |
} | |
} | |
class TempViewModel: ObservableObject { | |
@Published var sliderText = "" | |
@Published var sliderValue: Double = 0 | |
private var cancellable = Set<AnyCancellable>() | |
init() { | |
$sliderValue | |
.print("view model") | |
.sink { [weak self] value in | |
self?.sliderText = "\(value) C = \(String(format: "%.2f" ,value * 9 / 5 + 32)) F" | |
} | |
.store(in: &cancellable) | |
} | |
} | |
struct MyModalView: View { | |
@ObservedObject var viewModel: TempViewModel | |
var body: some View { | |
VStack(alignment: .leading) { | |
Text("SwiftUI Slider") | |
Slider(value: $viewModel.sliderValue, in: -100...100, step: 0.5) | |
.padding(.bottom) | |
Text("UIViewRepresentable Slider") | |
SwiftUISlider(minValue: -100, maxValue: 100, value: $viewModel.sliderValue) | |
Text(viewModel.sliderText) | |
} | |
.padding() | |
} | |
} | |
struct SwiftUISlider: UIViewRepresentable { | |
final class Coordinator: NSObject { | |
@Binding var value: Double | |
init(value: Binding<Double>) { | |
_value = value | |
} | |
@objc func valueChanged(_ sender: UISlider) { | |
let index = Int(sender.value + 0.5) | |
sender.value = Float(index) | |
print("value changed \(sender.value)") | |
self.value = Double(sender.value) | |
} | |
} | |
var minValue: Int = 0 | |
var maxValue: Int = 0 | |
@Binding var value: Double | |
func makeUIView(context: Context) -> UISlider { | |
let slider = UISlider(frame: .zero) | |
adapt(slider, context: context) | |
return slider | |
} | |
func updateUIView(_ uiView: UISlider, context: Context) { | |
adapt(uiView, context: context) | |
} | |
func makeCoordinator() -> SwiftUISlider.Coordinator { | |
Coordinator(value: $value) | |
} | |
private func adapt(_ slider: UISlider, context: Context) { | |
slider.minimumTrackTintColor = .systemRed | |
slider.maximumTrackTintColor = .systemRed | |
slider.maximumValue = Float(maxValue) | |
slider.minimumValue = Float(minValue) | |
slider.value = Float(value) | |
slider.addTarget( | |
context.coordinator, | |
action: #selector(Coordinator.valueChanged(_:)), | |
for: .valueChanged | |
) | |
} | |
} | |
struct PresentationMode_Previews: PreviewProvider { | |
static var previews: some View { | |
MyContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment