Last active
March 23, 2024 21:49
-
-
Save benigumocom/e70a029925e61765352c1129526a6d0c to your computer and use it in GitHub Desktop.
【SwiftUI】View の 強制再描画 👉 https://android.benigumo.com/20240324/force-redraw/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct TestView: View { | |
@State var id = false | |
var body: some View { | |
AutoProgressView() | |
.padding() | |
.id(id) | |
Button("restart") { | |
id.toggle() | |
} | |
.buttonStyle(.borderedProminent) | |
} | |
} | |
struct AutoProgressView: View { | |
@State private var value = 0.0 | |
private let total = 10.0 | |
private let duration = 1.0 | |
var body: some View { | |
ProgressView(value: value, total: total) | |
.task { | |
while value < total { | |
try? await Task.sleep(for: .seconds(duration)) | |
value += duration | |
} | |
} | |
} | |
} | |
#Preview { | |
TestView() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment