ios - 如何使 CareKitUI 条形图与 SwiftUI 兼容
问题描述
目前,使用 SwiftUI 对CareKit的支持有限。
我通常理解使对象符合 的想法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
需要设置为@Binding
结构中的变量UIViewRepresentable
,但我正在努力弄清楚我必须如何使用以下两个函数:
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
}
}
推荐阅读
- visual-c++ - 与结构成员变量关联的对话框控件
- javascript - Highcharts - PieChart 在调用 this.setState 的点击事件上刷新
- c++ - 试图转换 wxVector 的一个组件
这是 wxString 到 wxStringArray 但程序不想 - python - 遍历 pandas 中的列,同时对每列应用不同的函数
- go - 满足多个接口的可链接结构方法?
- java - 是否可以根据客户端的某些输入创建确定性的安全随机字符串?
- amazon-web-services - Amazon S3 Bucket Policy Block User Agent
- java - Spring Boot fetch lazy - 无法编写 JSON:无法初始化代理 - 没有会话
- proxy - 是否可以将 html get 请求从 create-react-app 代理到 /graphql?
- django - 如何将数据从 html 表单存储到 postgres 数据库?