[英]Is there a way to dismiss a modal view without animation in SwiftUI?
[英]How to create animation with delay when dismiss view swiftui
我在我的代碼中使用了以下示例( iOS SwiftUI:以編程方式彈出或關閉視圖),但我不知道如何創建動畫,就像翻頁一樣,並在點擊 [Button] 時延遲幾秒鍾。有誰知道解決方案?
struct DetailView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
Button(
"Here is Detail View. Tap to go back.",
action: {
//withAnimation(.linear(duration: 5).delay(5))// Error occurred in dalay.(Type of expression is ambiguous without more context)
withAnimation(.linear(duration: 5)) // not work
{
self.presentationMode.wrappedValue.dismiss()
}
}
)
}
}
struct RootView: View {
var body: some View {
VStack {
NavigationLink(destination: DetailView())
{ Text("I am Root. Tap for Detail View.")
}
}
}
struct ContentView: View {
var body: some View {
NavigationView {
RootView()
}
}
}
這將是一種方法。 如果沒有NavigationLink
您可以完全控制所有動畫和過渡。
struct DetailView: View {
@Binding var showDetail:Bool
var body: some View {
Button(
"Here is Detail View. Tap to go back.",
action: {
withAnimation(Animation.linear.delay(2)){
self.showDetail = false
}
}
).frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity).background(Color.yellow)
}
}
struct RootView: View {
@State var showDetail = false
var body: some View {
VStack {
if showDetail{
DetailView(showDetail:self.$showDetail).transition(.move(edge: .trailing))
}else{
Button("I am Root. Tap for Detail View."){
withAnimation(.linear){
self.showDetail = true
}
}
}
}.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity).background(Color.red)
}
}
你可以dispatchQueue延遲嗎?
Button(
"Here is Detail View. Tap to go back.",
action: {
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
self.presentationMode.wrappedValue.dismiss()
}})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.