In the following SwiftUI code I have 3 main components:
TestView - Holds a TestClass instance, displays MyShape, and updates the count of the TestClass instance on tap.
TestClass - A simple class to store a count, which gets published when it's changed.
MyShape - A simple shape whose position (should) change as the count of the TestClass instance in TestView changes.
The issue is that the position of MyShape does not change when the count updates.
I know that the count is being updated because if I display the count in a Text view it changes on tap, so while the count is changing, the shape is not updating when the count changes. Why is the shape not updating / why is the publisher not reaching the shape?
struct TestView: View {
@ObservedObject var counter: TestClass = TestClass()
var body: some View {
ZStack {
MyShape(counter: counter)
.stroke(Color.red, lineWidth: 50)
.onTapGesture {
counter.count += 1
}
}
}
}
class TestClass: ObservableObject {
@Published var count = 0
}
struct MyShape: Shape {
var counter: TestClass
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: counter.count, y: counter.count))
return path
}
}