如何在 SwiftUI 中更改路径颜色?

Bat*_*han 5 drawing flow colors swiftui

我仍然希望得到以下问题的答案。为了清楚起见,我添加了一些评论。

下面的代码通过 (Bezier) 路径生成圆的不同部分,这些路径通过 ZStack 一起连接成 3 个圆的单个图片,其中 25% 被切掉。我想根据 i 和 j 的值(总共 9 个)为每个段赋予不同的颜色。

这似乎是不可能的:在 Contentview 结构和 GeometryReader 中更改前景色都没有任何影响。任何想法如何做到这一点?

第二个问题:我尝试使用“switch”命令,但编译器抱怨“包含控制流语句的闭包不能与函数构建器‘ViewBuilder’一起使用”。不能使用流量控制对我来说似乎很奇怪。我错过了什么或做错了什么?

struct ContentView: View {

   static let segmentCount = 4
   static let circleCount = 4

    var body: some View {
        ZStack {
            ForEach(1..<ContentView.circleCount){ j in
               ForEach(1..<ContentView.segmentCount){i in
                  GeometryReader { geometry in
                     arcShape(i: i, j: j)
                     .scaleEffect(4.0 / 4.0, anchor: .top)
                     .foregroundColor(Color(red: (79.0 + Double(j) ) / 255, green: 79.0 / 255, blue: 191.0 / 255))
                  }
               }
            }
        }
    }
}


struct arcShape: View {

   let i: Int
   let j: Int

   static let arcColor2 =  Color(red: 79.0 / 255, green: 79.0 / 255, blue: 191.0 / 255)
   static let arcColor3 =  Color(red: 79.0 / 255, green: 120.0 / 255, blue: 191.0 / 255)
   // Create new path

   var body: some View {
      GeometryReader { geometry in
         Path {path in
            let center_x = CGFloat(200.0)
            let center_y = CGFloat(200.0)
            let r = CGFloat(50.0 + (CGFloat(self.j) - 1.0) * 50.0)
            let arc_start = CGFloat((45.0 + (CGFloat(self.i) - 1.0) * 90.0)) * CGFloat(Double.pi) / 180.0
            let arc_length = CGFloat(90.0 * CGFloat(Double.pi) / 180.0)
            let arc_width = CGFloat(25.0)
            let line0Target_x = center_x + r * CGFloat(cos(Double(arc_start)))
            let line0Target_y = center_y + r * CGFloat(sin(Double(arc_start)))
            let line1Target_x = center_x + (r + arc_width) * CGFloat(cos(Double(arc_start + arc_length)))
            let line1Target_y = center_x + (r + arc_width) * CGFloat(sin(Double(arc_start + arc_length)))
            let arcColor1 =  Color(red: (79.0 + Double(self.i) * 10.0) / 255, green: 79.0 / 255, blue: 79.0 / 255)

            path.move(to: CGPoint(x: line0Target_x, y: line0Target_y))
            path.addArc(center: CGPoint(x: center_x, y: center_y), radius: r, startAngle: Angle(radians: Double(arc_start)), endAngle: Angle(radians: Double(arc_start + arc_length)), clockwise: false)
            path.addLine(to: CGPoint(x: line1Target_x, y: line1Target_y))
            path.addArc(center: CGPoint(x: center_x, y: center_y), radius: (r + arc_width), startAngle: Angle(radians: Double(arc_start + arc_length)), endAngle: Angle(radians: Double(arc_start)), clockwise: true)
            path.addLine(to: CGPoint(x: line0Target_x, y: line0Target_y))
            path.foregroundColor(arcColor1)
         }
         .foregroundColor(Color.green)
//         .border(Color.red, width: 8)
//         switch i {
//         case 1: .fill(Self.arcColor1)
//         case 2: .fill(Self.arcColor2)
//         case 3: .fill(Self.arcColor3)
//         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)