每次我点击按钮时,我都希望出现一个新的cardView。我想知道这在swiftUI中是否可能,如果是这样,我需要做什么。如果我能把一些参数传递给cardView结构体就更好了,但是任何帮助都是很好的!
struct ContentView: View {
var body: some View {
ZStack {
VStack {
TextButton(action: {print("Button tapped")}, text: "new card")
CardView()
}
}
}
}
struct CardView: View {
var body: some View {
ZStack {
Rectangle()
.fill(Color(#colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1)))
.frame(width: 100, height: 100 * 1.618)
.cornerRadius(16)
.shadow(color: Color(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)).opacity(0.1), radius: 1, x: 0, y: 1)
.shadow(color: Color(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)).opacity(0.2), radius: 10, x: 0, y: 10)
VStack {
Text("Card")
.font(.system(size: 10) )
.foregroundColor(.white)
.bold()
}
}
}
}
struct TextButton: View {
let action: () -> Void
let text: String
var body: some View {
Button(action: action, label: {
Text(text)
.padding(.horizontal, 16)
.padding(.vertical, 16)
.foregroundColor(.white)
.background(Color.blue)
.cornerRadius(.infinity)
.shadow(color: Color(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)).opacity(0.1), radius: 1, x: 0, y: 1)
.shadow(color: Color(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)).opacity(0.2), radius: 10, x: 0, y: 10)
})
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
} ```
2条答案
按热度按时间z9gpfhce1#
在SwiftUI中,View反映的是State数据,所以你不是直接修改View,而是修改State数据,然后根据State构建View。这是SwiftUI背后的核心原则,它允许您将视图关注点与驱动它的数据(某些术语中的ViewModel)分开。
因此,让我们假设我们有一个
Card
的数据模型(使其符合Identifiable
-这将在稍后需要):让我们在View中定义一个卡片数组作为状态变量:
然后,主体可以用
ForEach
或List
视图显示这些卡:按钮的闭包向
cards
状态变量添加了一个新的Card
示例。就是这样。它不会直接改变视图中的任何内容。View会看到更改(这就是SwiftUI在幕后所做的)并重新呈现自己。您需要
Card
来符合Identifiable
的原因是让ForEach
知道如何唯一地标识每张卡。在不符合identifiable的情况下,您可以使用这样的密钥路径:sd2nnvve2#
看看这个