【发布时间】:2021-01-30 20:48:50
【问题描述】:
我正在创建应用程序并收到以下错误:线程 1:EXC_BAD_ACCESS (code=2, address=0x7ffeead03e18)。整个代码运行没有任何错误,只是当我在几秒钟后尝试启动应用程序时才收到此错误。
这是我的应用程序的代码:
import SwiftUI
struct Flower: Shape {
var petalOffset: Double = -20
var petalWidth: Double = 100
func path(in rect: CGRect) -> Path {
var path = Path()
for number in stride(from: 0, to: CGFloat.pi * 2, by: CGFloat.pi / 8) {
let rotation = CGAffineTransform(rotationAngle: number)
let position = rotation.concatenating(CGAffineTransform(translationX: rect.width / 2, y: rect.height / 2))
let originalPetal = Path(ellipseIn: CGRect(x: CGFloat(petalOffset), y: 0, width: CGFloat(petalWidth), height: rect.width / 2))
let rotatedPetal = originalPetal.applying(position)
path.addPath(rotatedPetal)
}
return path
}
}
struct ContentView: View {
@State private var petalOffset = -20.0
@State private var petalWidth = 100.0
var body: some View {
VStack {
Flower(petalOffset: petalOffset, petalWidth: petalWidth)
.stroke(Color.red, lineWidth: 1)
Text("Offset")
Slider(value: $petalOffset, in: -40 ... 40)
padding([.horizontal, .bottom])
Text("Width")
Slider(value: $petalWidth, in: 0 ... 100)
.padding(.horizontal)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
有人知道如何解决这个问题吗?
【问题讨论】:
标签: ios xcode error-handling swiftui cgaffinetransform