目标:
1- 显示一个覆盖整个屏幕的视图(蓝色) 2- 当点击底部(右上角)时,它会显示一个 HStack 动画右侧 HStack(绿色)“幻灯片偏移动画”。
import SwiftUI
struct ContentView: View {
@State var showgreen = false
var body: some View {
NavigationView {
HStack {
Rectangle()
.foregroundColor(.blue)
if showgreen {
Rectangle()
.foregroundColor(.green)
.offset(x: showgreen ? 0 : UIScreen.main.bounds.width)
.animation(.easeInOut)
}
}
.navigationBarItems(trailing:
Button(action: { self.showgreen.toggle() }) {
Image(systemName: "ellipsis")
})
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.colorScheme(.dark)
.previewDevice("iPad Pro (12.9-inch) (3rd generation)")
}
}
该代码有效,但我无法让绿色“幻灯片偏移动画”工作。非常感谢任何帮助!:)