swift - Dismiss modal automatically after 1 sec of being shown - swiftui
问题描述
is there a way to dismiss a modal(.sheet) automatically after 1 sec of being shown? without any user intervention.
I want to show it coming from the bottom, 1 sec, then gone (if possible with animation going up)
Basically I have 1 view showing a match information, then I click next or dislike and I want to show a fullScreenCover for 1 sec, to hide my ugly scroll to the top to show the next match. it's the same view but showing a new element in an array
thanks / merci / gracias
ScrollViewReader {ProxyReader in
ScrollView(.vertical, showsIndicators: false, content: {
Group {
//CustomImageView(urlString: model.matches[index].imageUrl3 ?? "")
if model.matches[index].imageUrl3 == "" {
Image(systemName: "person")
.frame(width: 300, height: 300, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
.aspectRatio(contentMode: .fit)
} else {
RemoteImage(url: model.matches[index].imageUrl3!)
.aspectRatio(contentMode: .fit)
.frame(width: 200)
}
Text(model.matches[index].bucketList)
}.padding()
})//scroll view
//to recreate the veiw from scratch
.id(self.scrollViewID)
//this is to show the rejection button
.overlay(
Button(action: {
//move to the next match
self.isPresented.toggle()
if self.index == model.matches.count-1 {
//go back to first match
self.index = 0
} else {
self.index += 1
}
//scroll to top
withAnimation(.spring()) {
ProxyReader.scrollTo("SCROLL_TO_TOP", anchor: .top)
//how to dismiss and delay animation for 2 secs!!
self.isPresented.toggle()
}
}, label: {
Image(systemName: "xmark.circle.fill")
.font(.system(size:50, weight: .semibold))
.foregroundColor(.white)
.padding()
.background(Color("red"))
.clipShape(Circle())
})
.padding(.trailing)
.padding(.bottom, getSafeArea().bottom == 0 ? 12 : 0) //this is an if statement
//.opacity(-scrollViewOffset > 450 ? 1 : 0)
.animation(.easeInOut)
//to show rejection transition
.fullScreenCover(isPresented: $isPresented, content: {
FullScreenModalView.init(
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
isPresented = false
}
)
})
解决方案
If I get what you are asking, a solution is to use DispatchQueue.main.asyncAfter()
:
//to show rejection transition
.fullScreenCover(isPresented: $isPresented, content: {
FullScreenModalView.init()
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
isPresented = false
}
})
Please note that this may not be exactly precise as the firing time depends upon how busy the OS is at that moment, but it will generally be very good. Also, you generally want some user interaction in dismissing a modal as the user may look away in the second you display it.
推荐阅读
- amazon-dynamodb - AppSync 查询解析器:是否需要 expressionNames 和 expressionValues?
- javascript - 在异步函数中从 catch 返回或跳过
- go - UUID 的 Sha1 hex - 用作访问令牌
- ruby-on-rails - RoR - 从数据库中删除的问题
- python - 在 PyCharm 中导入 numpy python 模块失败
- javascript - Javascript 2D 数组问题更改特定元素更改列
- codenameone - 如何构建用于平板电脑表单布局的 CN1 代码?
- c - MPI_Gather 发送和接收数组错误
- java - 尝试从 Json 到 String 获取值时出现 JSONException
- python - 地图未显示在 Jupyter Notebook 中