【问题标题】:Drawing a line chart in SwiftUI在 SwiftUI 中绘制折线图
【发布时间】:2020-07-20 12:30:32
【问题描述】:

我正在尝试在 Swift UI 中绘制折线图,​​但无法实现 for 循环:

Path { path in
    path.move(to: CGPoint(x: 20, y: 20))
    ForEach(0 ..< 11) { index in
    path.addLine(to: CGPoint(x: ((index * 20) + 40), y: Int(hr[index])))
}

给我一​​个构建错误:

类型'()'不能符合'View';只有结构/枚举/类类型可以 符合协议

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    这是一个解决方案的演示:

    Path { path in
        path.move(to: CGPoint(x: 20, y: 20))
        for index in 0 ..< 11 {
            path.addLine(to: CGPoint(x: ((index * 20) + 40), y: y_pos[index]))
        }
    }
    // .stroke(Color.red) // for demo stroked if inside ViewBuilder
    

    【讨论】:

      猜你喜欢
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-12
      • 1970-01-01
      相关资源
      最近更新 更多