您好,我正在try 一个快速的用户界面过渡,可以点击一个按钮,当视图2从顶部向下滑到屏幕上时,视图1将从屏幕底部向下滑动.如果显示了视图2,则当视图1从屏幕底部向上滑动时,视图2将向上滑出屏幕顶部.我try 使用自定义过渡来执行此操作,但这些都不起作用.在动画中包装布尔值更改也不起作用.我怎么才能解决这个问题呢?
import SwiftUI
extension AnyTransition {
static var moveOne: AnyTransition {
AnyTransition
.asymmetric(
insertion: .move(edge: .bottom),
removal: .move(edge: .top)
)
}
static var moveTwo: AnyTransition {
AnyTransition
.asymmetric(
insertion: .move(edge: .top),
removal: .move(edge: .bottom)
)
}
}
struct SwiftUIView: View {
@State var show = true
var body: some View {
if !show {
Rectangle().fill(.red.gradient).frame(width: widthOrHeight(width: true) * 0.8, height: widthOrHeight(width: false)).transition(.moveOne)
.onTapGesture {
show.toggle()
}
} else {
Rectangle().fill(.blue.gradient).frame(width: widthOrHeight(width: true) * 0.8, height: widthOrHeight(width: false)).transition(.moveTwo)
.onTapGesture {
show.toggle()
}
}
}
}
func widthOrHeight(width: Bool) -> CGFloat {
let scenes = UIApplication.shared.connectedScenes
let windowScene = scenes.first as? UIWindowScene
let window = windowScene?.windows.first
if width {
return window?.screen.bounds.width ?? 0
} else {
return window?.screen.bounds.height ?? 0
}
}
struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
SwiftUIView()
}
}