我正在尝试使用 SwiftUI 来更好地理解它,但我无法理解这种行为上的差异。在此示例代码中,为什么结构视图能够旋转而 @ViewBuilder 函数却不能旋转?
struct ContentView: View {
@State var isSpinning = false
var body: some View {
StructView()
funcView()
}
@ViewBuilder
func funcView() -> some View {
@State var isSpinning = false
VStack {
Button {
isSpinning.toggle()
}label: {
Text("Spin me")
}
Image(systemName: "arrow.clockwise")
.font(.system(size: 80))
.foregroundColor(.blue)
.rotationEffect(.degrees(isSpinning ? 360 : 0))
.animation(.easeInOut(duration: 1), value: isSpinning)
}
.padding()
}
struct StructView: View {
@State var isSpinning = false
var body: some View {
VStack {
Button {
isSpinning.toggle()
}label: {
Text("Spin me")
}
Image(systemName: "arrow.clockwise")
.font(.system(size: 80))
.foregroundColor(.blue)
.rotationEffect(.degrees(isSpinning ? 360 : 0))
.animation(.easeInOut(duration: 1), value: isSpinning)
}
.padding()
}
}
}