有没有办法在 SwiftUI 中为新的全屏模式 iOS 14 使用替代动画?
目前它从底部向上滑动,但我想要交叉溶解。我已经尝试了几件事,但没有运气。我在想新的 matchedGeometryEffect()
修饰符可能有用。
下面是这个新功能的默认使用
struct ContentView: View {
@State private var isShowing = false
var body: some View {
Button {
isShowing.toggle()
} label: {
Text("Show Modal").font(.largeTitle)
}.fullScreenCover(isPresented: $isShowing) {
Text("Hello").font(.largeTitle)
}
}
}
最佳答案
现在没有直接的 API 来执行此操作,但您可以使用 ZStack 进行简单的 hack 来解决此问题
波纹管代码作为替代品工作得很好
@State var isPresented = false
var body: some View {
ZStack {
NavigationView {
VStack {
Button(action: {
// trigger modal presentation
withAnimation {
self.isPresented.toggle()
}
}, label: {
Text("Show standard modal")
})
}.navigationBarTitle("Standard")
}
ZStack {
HStack {
Spacer()
VStack {
HStack {
Button(action: {
// trigger modal presentation
withAnimation {
self.isPresented.toggle()
}
}, label: {
Text("Dismiss")
.font(.headline)
.foregroundColor(.white)
})
Spacer()
Image(systemName: "xmark.circle.fill")
.foregroundColor(.white)
.onTapGesture {
withAnimation {
self.isPresented.toggle()
}
}
}
.padding(.top, UIApplication.shared.windows.filter{$0.isKeyWindow}.first?.safeAreaInsets.top)
Spacer()
}
Spacer()
}
}.background(Color.yellow)
.edgesIgnoringSafeArea(.all)
.offset(x: 0, y: self.isPresented ? 0 : UIApplication.shared.keyWindow?.frame.height ?? 0)
}
}
关于animation - 全屏封面/模态的替代动画 - iOS 14,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62593549/