当进度条动画结束时,我无法切换到下一个屏幕
I can't change on to the next screen when progress bar animation is over
@EnvironmentObject var viewRouter: ViewRouter
@State var percent: CGFloat = 0
var body: some View {
VStack {
ProgressBar(width:200, height: 30, percent: percent)
.onAppear(perform: {
withAnimation(Animation.easeIn.speed(0.1)) {
while percent < 100 {
percent += 1
}
viewRouter.currentPage = .homepage
}
})
}
}
动画没有结束但立即将视图更改为.homepage。
另一个编辑:尝试类似这样的测试代码:
import SwiftUI
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
//@EnvironmentObject var viewRouter: ViewRouter
@State var percent: CGFloat = 0
@State var showHomePage = false
func doProgress() {
if percent < 100 {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.02) {
percent += 1
doProgress()
}
} else {
showHomePage.toggle()
// viewRouter.currentPage = .homepage
}
}
var body: some View {
NavigationView {
VStack {
ProgressView("", value: percent, total: 100)
.onAppear {
doProgress()
}
NavigationLink(
destination: Text("the next screen view"),
isActive: $showHomePage,
label: { EmptyView() } )
}
}
}
}
@EnvironmentObject var viewRouter: ViewRouter
@State var percent: CGFloat = 0
var body: some View {
VStack {
ProgressBar(width:200, height: 30, percent: percent)
.onAppear(perform: {
withAnimation(Animation.easeIn.speed(0.1)) {
while percent < 100 {
percent += 1
}
viewRouter.currentPage = .homepage
}
})
}
}
动画没有结束但立即将视图更改为.homepage。
另一个编辑:尝试类似这样的测试代码:
import SwiftUI
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
//@EnvironmentObject var viewRouter: ViewRouter
@State var percent: CGFloat = 0
@State var showHomePage = false
func doProgress() {
if percent < 100 {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.02) {
percent += 1
doProgress()
}
} else {
showHomePage.toggle()
// viewRouter.currentPage = .homepage
}
}
var body: some View {
NavigationView {
VStack {
ProgressView("", value: percent, total: 100)
.onAppear {
doProgress()
}
NavigationLink(
destination: Text("the next screen view"),
isActive: $showHomePage,
label: { EmptyView() } )
}
}
}
}