我将一平方米的面积推到下面的泥.中。 我的看法是,要确定究竟是选择一个圈子还是哪一个广场,就必须遵守<条码>。 <代码>MyData#doSomethingExpensive 方法旨在模拟一项长期任务,在完成这项任务后,该任务将启动ID至业者(在这种情况下,将一平方米)。
This actually works as intended, but I have a withAnimation
call in my @Observable
UI model. I would rather have withAnimation
inside the view, if possible. I would also like to continue using the @Observable
API instead of switching back to ObservableObject
conformance. Is there a way to refactor this such that the view will respond to changes in isCircle
with the same animation that I have here:
import SwiftUI
@Observable
final class MyData {
var isCircle = false
func doSomethingExpensive() {
Task {
try await Task.sleep(for: .milliseconds(Int.random(in: 300...800)))
withAnimation(.smooth(duration: 1.5)) {
self.isCircle.toggle()
}
}
}
}
struct ContentView: View {
let myData = MyData()
var body: some View {
VStack {
if myData.isCircle {
Circle().fill(.blue).frame(width: 200)
} else {
Rectangle().fill(.red).frame(width: 200, height: 200)
}
Button("Animate later") {
myData.doSomethingExpensive()
}
}
}
}