[英]How to make CareKitUI Bar Charts compatible with SwiftUI
目前,有限的支持CareKit使用SwiftUI。
一般來說,我理解使對象符合UIViewRepresentable
,但我正在努力了解這在實踐中的工作方式。
以下是自述文件中的示例代碼:
let chartView = OCKCartesianChartView(type: .bar)
chartView.headerView.titleLabel.text = "Doxylamine"
chartView.graphView.dataSeries = [
OCKDataSeries(values: [0, 1, 1, 2, 3, 3, 2], title: "Doxylamine")
]
因此init(type)
、 headerView.titleLabel
和graphView.dataSeries
需要在符合UIViewRepresentable
的結構中設置為@Binding
變量,但我正在努力弄清楚我必須如何使用以下兩個函數:
func makeUIView() {}
func updateUIView() {}
任何幫助將非常感激。
實際上只有數據需要綁定,因為類型是初始化的一部分,標題幾乎不可能改變,所以這里有可能的變體
struct CartesianChartView: UIViewRepresentable {
var title: String
var type: OCKCartesianGraphView.PlotType = .bar
@Binding var data: [OCKDataSeries]
func makeUIView(context: Context) -> OCKCartesianChartView {
let chartView = OCKCartesianChartView(type: type)
chartView.headerView.titleLabel.text = title
chartView.graphView.dataSeries = data
return chartView
}
func updateUIView(_ uiView: OCKCartesianChartView, context: Context) {
// will be called when bound data changed, so update internal
// graph here when external dataset changed
uiView.graphView.dataSeries = data
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.