CardView 是一个功能丰富的 SwiftUI 组件,用于以卡片格式显示视图集合。它支持网格和水平滚动布局,并包含可选的缩放效果。
通过 Swift Package Manager 将包添加到您的项目中 https://swiftpackageindex.cn/softsuave-tech-matrix/adaptive_card_layout
或者,您也可以通过 Cocoapods 安装,pod 'adaptive_card_layout'
步骤 1
import SwiftUI
struct CustomView1: View {
var text: String
var color: Color
var image: String
var body: some View {
VStack(alignment:.center) {
Text(text)
.font(.title)
.foregroundColor(.white)
Image(systemName: image)
.resizable()
.frame(width: 50, height: 50)
.foregroundColor(.yellow)
}
.frame(width: 180, height: 300)
.background(color)
.cornerRadius(10)
.padding()
}
}
步骤 2
在您的父视图中导入 CardViewAdvanced 库
根据您的需求创建一个视图数组
private var imageArray: [String:String] = ["CustomView 1":"magnifyingglass","CustomView 32":"moon.fill","CustomView 23":"sun.min","CustomView 33": "cloud","CustomView 4":"moon","CustomView 5":"sun.max","CustomView 6":"sun.snow", "CustomView 7":"sun.max.fill", "CustomView 8":"moon.zzz", "CustomView 92":"cloud.snow.fill", "CustomView 22":"cloud.snow", "CustomView 11":"sun.snow.fill","CustomView 12": "cloud.fog", "CustomView 21":"magnifyingglass","CustomView 2":"moon.fill","CustomView 3":"sun.min","CustomView 13": "cloud","CustomView 14":"moon","CustomView 15":"sun.max","CustomView 16":"sun.snow", "CustomView 17":"sun.max.fill", "CustomView 18":"moon.zzz", "CustomView 9":"cloud.snow.fill", "CustomView 10":"cloud.snow", "CustomView 19":"sun.snow.fill","CustomView 20": "cloud.fog"]
@State private var viewsArray: [AnyView] = []
init() {
_viewsArray = State(initialValue: imageArray.map { item in
AnyView(CustomView1(text: item.key, color: Color.random, image: item.value))
})
}
步骤 3
CardView(viewsArray: $viewsArray, scale: true, frameWidth: 180, frameHeight: 300,additionalFrameHeight: 30, gridView: $isGridView, RowOrcolumnNo: $columnNo) { indexNo in
print(indexNo)
}
欢迎任何贡献!对于重大更改,请先打开一个 issue 讨论您想要更改的内容。
请确保适当地更新测试。
如果您有任何反馈,请通过 techmatrix@softsuave.com 联系我们