在SwiftUI中绘制折线图

时间:2020-07-20 12:30:32

标签: swiftui

我正在尝试在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])))
}

给我一​​个构建错误:

类型'()'不符合'视图';只有struct / enum / class类型可以 符合协议

1 个答案:

答案 0 :(得分: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