Last active
July 6, 2023 22:51
-
-
Save aheze/f53c937324a58ed8d15a082cd63abd43 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 | |
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