首页 > 解决方案 > 在 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])))
}

给我一个构建错误:

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

标签: swiftui

解决方案


这是解决方案的演示:

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

推荐阅读