Interestingly enough, you can get animatableData to work if you mark the computed variable as nonisolated. Xcode will fail at previewing the result, but a live run shows it working..
struct ContentView: View {
static let bigAngle = 60.0
static let smallAngle = 10.0
@State var angle = Self.bigAngle
var body: some View {
MyShape(angle: angle)
.foregroundStyle(.tint)
.frame(width: 300, height: 200)
.animation(.smooth(duration: 1.0), value: angle)
.onTapGesture {
angle = angle == Self.bigAngle ? Self.smallAngle : Self.bigAngle
}
}
}
struct MyShape: Shape {
init(angle: Double) {
self.angle = angle
}
var angle: Double
nonisolated var animatableData: Double {
get {angle}
set {angle = newValue }
}
func path(in rect: CGRect) -> Path {
Path { path in
path.move(to: CGPoint(x: 0, y: 0)) // top left
path.addLine(to: CGPoint(x: max(0,rect.width - rect.height*tan(angle * .pi/180)), y: 0)) // top right (or top left if the angle is large enough)
path.addLine(to: CGPoint(x: rect.width, y: rect.height)) //bottom right
path.addLine(to: CGPoint(x: 0, y: rect.height)) // bottom left
path.closeSubpath() // Close (back to top left)
}
}
}
Post
Replies
Boosts
Views
Activity
ideally the protocols would be updated and restricted to main actor, but I don’t hold out much hope for that based on other threads I have read.