Created
August 17, 2025 00:39
-
-
Save omochi/967fc25720e389043bfe85e420f480c9 to your computer and use it in GitHub Desktop.
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 | |
| @Observable | |
| final class Controller { | |
| init() {} | |
| let cooldown0 = 0.2 | |
| let cooldown1 = 0.2 | |
| var showsAlert: Bool = false | |
| var count: Int = 0 | |
| func start() { | |
| Task { | |
| count += 1 | |
| showsAlert = true | |
| try await Task.sleep(for: .seconds(cooldown0)) | |
| showsAlert = false | |
| try await Task.sleep(for: .seconds(cooldown1)) | |
| count += 1 | |
| showsAlert = true | |
| } | |
| } | |
| } | |
| struct ContentView: View { | |
| @State var controller: Controller = .init() | |
| var body: some View { | |
| VStack { | |
| Button("tap me") { | |
| controller.start() | |
| } | |
| } | |
| .padding() | |
| .alert( | |
| "alert \(controller.count)", | |
| isPresented: $controller.showsAlert, | |
| actions: { } | |
| ) | |
| } | |
| } | |
| #Preview { | |
| ContentView() | |
| } |
Author
omochi
commented
Aug 17, 2025
Author
ちょっと遅らせると動く・・・
func update() {
guard controller.showsAlert == false else { return }
guard controller.step < 10 else { return }
guard controller.buttonCount == controller.step else { return }
print(controller.showsAlert, controller.step, controller.buttonCount)
controller.step += 1
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
controller.showsAlert = true
}
}なんと、withAnimationのcompletionを使えば完全に動くっぽいです。Simulatorのスローアニメーションにしても動きました。
func update() {
guard controller.showsAlert == false else { return }
guard controller.step <= 5 else { return }
guard controller.buttonCount == controller.step else { return }
print(controller.showsAlert, controller.step, controller.buttonCount)
withAnimation {
controller.step += 1
} completion: {
controller.showsAlert = true
}
}↓ではダメ。
withAnimation {
//
} completion: {
controller.step += 1
controller.showsAlert = true
}
Author
現状、update()を呼ぶ経路が2つあるので、stepは即座に更新しないと色々おかしくなりますね。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment