我正在尝试制作一个图形应用程序,但是出于某种原因,使用@State属性对其进行动画处理没有帮助。
struct GraphBars: View {
@State var percent: CGFloat
var body: some View {
Capsule()
.fill(Color.black)
.frame(width: 50, height: self.percent)
}
}
struct TEST3: View {
@State var bar1: CGFloat = 90.0
var body: some View {
GeometryReader { gg in
VStack {
Button(action: {
self.bar1 = 300.0
}) {
Text("Hello")
}
GraphBars(percent: bar1)
}
但是,按下按钮并不会像我想的那样增加条形的高度。我在做什么错了?
答案 0 :(得分:1)
您需要使用@Binding在两个视图之间来回传递变量,否则父视图不会收到有关变量更改的通知。
这样做:
struct GraphBars: View {
@Binding var percent: CGFloat
var body: some View {
Capsule()
.fill(Color.black)
.frame(width: 50, height: self.percent)
}
}
struct Test3: View {
@State var bar1: CGFloat = 90.0
var body: some View {
GeometryReader { gg in
VStack {
Button(action: {
self.bar1 = 300.0
}) {
Text("Hello")
}
GraphBars(percent: self.$bar1)
}
}
}
}