我尝试了很多事情,但是似乎收到了无法删除的连续错误消息。该代码在下面列出,并且我有以下问题:
对初学者的任何帮助将不胜感激。
struct ContentView: View {
static let segmentCount = 4
static let circleCount = 2
var body: some View {
let r = CGFloat(75.0)
let center_x = CGFloat(150.0)
let center_y = CGFloat(150.0)
var arc_start = CGFloat(45.0 * CGFloat(Double.pi) / 180.0)
let arc_length = CGFloat(90.0 * CGFloat(Double.pi) / 180.0)
var 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)))
ZStack {
ForEach(1..<ContentView.circleCount){ j in
r = CGFloat(25.0 + CGFloat((j) - 1.0) * 25.0)
ForEach(1..<ContentView.segmentCount){ i in
Path { path in
arc_start = CGFloat((45.0 + (CGFloat(i) - 1.0) * 90.0)) * CGFloat(Double.pi) / 180.0
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.closeSubpath()
// let shapeLayer = CAShapeLayer()
// shapeLayer.strokeColor = UIColor.red.cgColor
// shapeLayer.fillColor = UIColor.blue.cgColor
// shapeLayer.path = path.cgPath
}
}
}
}
}
}
答案 0 :(得分:0)
您的代码将无法编译,因为您在let
闭包中添加了var
和body
,因此编译器期望使用View
。
您可以将vars简单地移到Path
闭包内(或者可以创建一些函数来对其进行计算):
struct ContentView: View {
static let segmentCount = 4
static let circleCount = 2
var body: some View {
ZStack {
ForEach(1..<ContentView.circleCount){ j in
ForEach(1..<ContentView.segmentCount){ i in
Path { path in
let r = CGFloat(25.0 + (CGFloat(j) - 1.0) * 25.0)
let center_x = CGFloat(150.0)
let center_y = CGFloat(150.0)
var arc_start = CGFloat(45.0 * CGFloat(Double.pi) / 180.0)
let arc_length = CGFloat(90.0 * CGFloat(Double.pi) / 180.0)
var 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)))
arc_start = CGFloat((45.0 + (CGFloat(i) - 1.0) * 90.0)) * CGFloat(Double.pi) / 180.0
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.closeSubpath()
}
}
}
}
}
}