Skip to content

Instantly share code, notes, and snippets.

@aheze
Last active July 6, 2023 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aheze/f53c937324a58ed8d15a082cd63abd43 to your computer and use it in GitHub Desktop.
Save aheze/f53c937324a58ed8d15a082cd63abd43 to your computer and use it in GitHub Desktop.
import SwiftUI
import Combine
import SceneKit
class ViewModel: ObservableObject {
var changeColor = PassthroughSubject<UIColor, Never>()
var cancellables = Set<AnyCancellable>()
}
struct ContentView: View {
@StateObject var model = ViewModel()
var body: some View {
YourView(model: model)
Button("Change") {
model.changeColor.send(UIColor.blue)
}
}
}
struct YourView: UIViewRepresentable {
@ObservedObject var model: ViewModel
func makeUIView(context: Context) -> SCNView {
let view = SCNView()
model.changeColor
.sink { color in
view.backgroundColor = color
}
.store(in: &model.cancellables)
return view
}
func updateUIView(_ uiView: SCNView, context: Context) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment