ios - 如何在关闭视图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)
}
}
推荐阅读
- .net - 无法从 Mac 上的主机连接到 docker 容器
- python - 使用 Python 进行高效的张量收缩
- apache-kafka - Kafka - 每小时发布 80 亿条消息
- sql-server - SQL Server 意外死锁
- node.js - 我是否必须在网站项目中包含 UI 上所有库的列表?
- tensorflow - 如果 Google 的 WaveNet 模型已经将音频作为输入,为什么还需要它来生成音频?
- swift - WatchOS 陀螺仪校准中断
- java - Spring Boot 1.5 sessionRegistry().getAllPrincipals() 总是返回空列表
- java - 使用具有许多 Firebase 任务的许多匿名对象作为侦听器会显示对内存的影响吗?
- oracle - 我不断收到 Oracle 中 PL/SQL 的致命错误/重置连接