简体   繁体   中英

How to forcefully redraw a swift ui view within a button action call

So I'm doing something sort of jank, but I need to do it for a class that I am taking

I need to update the view after every step of a while loop that is inside of a button's action: callback, in order to create a really simple "animation"

is there any way to get a swift UI view to drop everything and redraw immediately?

code for my button:

                Button(action: {
                    pass_again = true;
                    while(pass_again){
                        (right_text, pass_again) = twos.collapse(dir: Direction.right)
                        cur_id+=1
                        usleep(1000000)
                        // I need to redraw the UI here 
                    }
                    twos.spawn()
                }, label: {
                    Text(right_text)
                }).padding(0.5)

You can bind an @State variable to any SwiftUI view ie Text(self.yourvar) , so any time you change the @State value it will automatically update the text or any other property bonded with it.

About the animation you can use .withAnimation modifier to create a simple animation that swiftUI will perform

withAnimation {
    your state var updates goes here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM