I am trying to make CGPoint array conform to VectorArithmetic using this piece of code.
https://gist.github.com/mecid/04ab91f45fec501e72e4d5fb02277f3f
but, only the first point moves with animation. I have no idea what is wrong…. Please anyone help me with this.
here is the full code.
struct ContentView4: View {
@State var points: AnimatableCGPointVector = .zero
var body: some View {
VStack(spacing: 24) {
GeometryReader {
let size = $0.size
LineShape(
points: points
)
.stroke(.orange, style: StrokeStyle(lineWidth: 32, lineCap: .round, lineJoin: .round))
.onAppear {
points = AnimatableCGPointVector(values: one(size: size))
}
.overlay {
HStack {
Button(action: {
withAnimation(.bouncy(duration: 0.4, extraBounce: 0.3)) {
points = AnimatableCGPointVector(values: one(size: size))
}
}, label: {
Text("one")
})
Button(action: {
withAnimation(.bouncy(duration: 0.4, extraBounce: 0.3)) {
points = AnimatableCGPointVector(values: two(size: size))
}
}, label: {
Text("two")
})
}
}
}
}
.frame(width: 320, height: 320)
}
func one(size: CGSize) -> [CGPoint] {
let p1 = CGPoint(x: 90, y: -70)
let p2 = CGPoint(x: 160, y: 42)
let p3 = CGPoint(x: 75, y: 30)
return [p1, p2, p3]
}
func two(size: CGSize) -> [CGPoint] {
let p1 = CGPoint(x: 40, y: 320)
let p2 = CGPoint(x: 80, y: 50)
let p3 = CGPoint(x: 25, y: 45)
return [p1, p2, p3]
}
}
struct LineShape: Shape {
var points: AnimatableCGPointVector
var animatableData: AnimatableCGPointVector {
get { points }
set { points = newValue }
}
func path(in rect: CGRect) -> Path {
return Path { p in
p.addLines(points.values)
}
}
}
I tried to do the best I can, but I am new to VectorArithmetic, so I am still learning what actually happens.