Hi iOS member,
I have a problem to use a SwiftUI on Carplay.
The swiftUI is not refreshed when @State properties are modified.
Only UiKit views refreshs well, on Carplay.
Is it exists a way to use swiftUI views on Carplay, for example to show a speed of the user on a Map application ?
Thank you for your help.
Here an example of SwiftUI view :
public struct FakeSpeedView: View {
@State var speed: Int = 90
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
public init() {}
public var body: some View {
Text("Speed \(speed)")
.onReceive(timer) { _ in
var newValue = speed + 1
if newValue > 130 {
newValue = 90
}
print("FakeSpeedView update speed \(newValue)")
speed = newValue
}
}
}
And here, the ViewController used as rootViewController for CPWindow :
class ContentViewController: UIViewController {
private var circularProgressBarView: CircularProgressBarView!
private var circularViewDuration: TimeInterval = 2
private var speedVC: UIViewController!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setUpSpeedView()
setUpCircularProgressBarView()
}
private func setUpSpeedView() {
speedVC = UIHostingController(rootView: FakeSpeedView())
view.addSubview(speedVC.view)
speedVC.view.translatesAutoresizingMaskIntoConstraints = false
speedVC.view.pinEdges(to: view)
addChild(speedVC)
speedVC.didMove(toParent: self)
}
private func setUpCircularProgressBarView() {
// set view
circularProgressBarView = CircularProgressBarView(frame: .zero)
// align to the center of the screen
circularProgressBarView.center = view.center
// call the animation with circularViewDuration
circularProgressBarView.progressAnimation(duration: circularViewDuration)
// add this view to the view controller
view.addSubview(circularProgressBarView)
}
}