Hello, news about SwiftUI have exited me, and I decided to play with it, and seems that I am stuck in primitive stuff, but I haven't really found how to solve it.
struct CellView: View {
@Binding var color: Int
@State private var padding : Length = 0
let colors = [Color.yellow, Color.red, Color.blue, Color.green]
var body: some View {
colors[color]
.cornerRadius(20)
.padding(padding)
.animation(.spring())
}
}
and parent view
struct ContentView: View {
@State private var array: [Int] = [1,2,3]
var body: some View {
VStack(alignment: .leading) {
ForEach(self.array.indices) { index in
CellView(color: self.$array[index])
.tapAction {
self.array = [1,1,1]
}
}
}
}
}
So basically, when user taps on CellView
I am changing colors of every CellView
. What I am strugling with, is that I want to add padding animation every time when value of color
property is changed for CellView
. I want to animate padding from 10 to 0 in this case. But I haven't found a way to somehow "listen" to color
property change. I've tried to use didSet
in color
property and do my manipulations with padding there, but it doesn't work. Am I missing something? Would appreciate any info about this.