釣魚術
SwiftUI
昨天使用 List{}
將變數中的魚名做成列表後,今天接著把畫完刻完。
來看看原始碼的部分...
ToxicFishListView.swift
source code//
// ContentView.swift
// BaoAnGongFisher
//
// Created by nipapa on 2022/9/7.
//
import SwiftUI
struct ToxicFishListView: View {
var toxicfishes = toxicFishesData
var body: some View {
VStack(spacing: 10){
HStack{
Text("危險魚類")
.background(.red)
.font(.title)
.frame(alignment: .topLeading)
Spacer()
}
List(toxicfishes, id: \.self) { toxicfish in
HStack { // 有毒的魚,會以水平方式堆疊其他元件
// 最左邊放一張圖片
Image(toxicfish.imageName)
// 放一個垂直堆疊
VStack {
// 魚的名字
Text(toxicfish.fishName)
.font(.headline)
.fontWeight(.bold)
// 魚的俗名,把 [String] join 成一個大的字串
Text(toxicfish.fishNickName.joined(separator: "、 "))
}
// 塞 Spacer 填滿
Spacer()
}
}
}
}
}
struct ToxicFishListView_Previews: PreviewProvider {
static var previews: some View {
ToxicFishListView()
}
}
ToxicFishListView.swift
說明透過巢狀式擺放元件
HStack
VStack
Spacer
Image
其他沒用到的東西先不玩不介紹了...
ToxicFish.swift
source code//
// ToxicFish.swift
// BaoAnGongFisher
//
// Created by nipapa on 2022/9/9.
//
import Foundation
struct FishRect: Hashable { // 放置魚資訊的方塊
var fishName:String // 魟、虎、沙毛、斑午、象耳、倒吊
var fishCategory:String // 魟
var fishNickName:[String] // 赤魟
var imageName:String {return fishName} // 赤土魟 ...
}
var toxicFishesData = [
FishRect(fishName: "赤魟", fishCategory: "魟", fishNickName: ["赤土魟", "紅魴魚", "牛尾魴"]),
FishRect(fishName: "黃魟", fishCategory: "魟", fishNickName: ["笨氏土魟", "黃魴", "紅魴"])
]
BaoAnGongFisher
目錄下,新建一個 swift
檔,命名成 ToxicFish.swift