English 中文(简体)
如何在“可保全财产”发生变化时迅速进行估算
原标题:How to perform a SwiftUI animation when an @Observable property changes

我将一平方米的面积推到下面的泥.中。 我的看法是,要确定究竟是选择一个圈子还是哪一个广场,就必须遵守<条码>。 <代码>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()
            }
        }
    }
}
问题回答

一种办法是,你可以移动<代码>。 任务__} to the view, such as:

@Observable
final class MyData {
    var isCircle = false
    
    func doSomethingExpensive() async throws {
        try await Task.sleep(for: .milliseconds(Int.random(in: 300...800))) // <--- here
    }
}

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") {
                Task { // <--- here
                    try await myData.doSomethingExpensive()
                    withAnimation(.smooth(duration: 1.5)) {
                        myData.isCircle.toggle()
                    }
                }
            }
        }
    }
}




相关问题
images sliding continuously with <table> and jQuery

I m trying to write a little test page that circulates images through a window (see image). I have colored boxes inside a table (gray border), with each box being a element. <table id="boxes" ...

WPF 3d rotation animations

I have a few 3d rectangles on my screen that I want to pivot around the Y axis. I want to press down with the mouse, and rotate the 3d object to a max rotation, but when the user moves their mouse, ...

Disable Windows 7 touch animation in WPF

In Windows 7 when you touch the screen there is a short animation that occurs at the touch point. In my WPF app I want to display my own touch points, without showing the one supplied by Windows. ...

jQuery block moving

I wanna move a div block to the top, so I coded like this: CSS part: .movingPart{ margin-top:80px; } jQuery part: $(document).ready(function() { $( #btn ).click(function() { $( .movingPart )....

Direct 2D gnuplot PNG animation?

Can anyone please confirm that yes/no Gnuplot 4.5 (on CVS) can output 2D animated PNG files? I have numerous datasets but one line that I d like to show iteratively in 3 different places in my graph. ...

Visual State Manager VS Animations in WPF

There is a lot of talk about the simplicity of Visual States and the transitions between them in WPF/Silverlight. I have the need to generate animations dynamically at runtime, to animate the ...

Create a ImageIcon that is the mirror of another one

I ll like to know if there is a way to create a ImageIcon that is the mirror of another ImageIcon. Searching on Google, I found how to do it by using many AWT libraries. Is there a way to do it with ...

how to drag in the animation in iphone application?

Iphone application is using the static co-ordinates to reach at some points in the application,which is based on the button event. But what i want is when i drag the item than it should reach at some ...

热门标签