cs193p-memorize/Memorize/ContentView.swift
Ching 189bfc0bb8 feat(view, viewmodel): 修改 veiwmodel 发送用户点击事件,修改 card 状态,完成 card match 逻辑
点击 card 修改 card.isFaceUp 属性,当两个 card.content 相同时,将 card 隐藏

Signed-off-by: Ching <loooching@gmail.com>
2023-02-12 20:55:45 +08:00

55 lines
1.5 KiB
Swift

//
// ContentView.swift
// Memorize
//
// Created by ching on 2023/2/12.
//
import SwiftUI
struct ContentView: View {
@ObservedObject var viewModel: EmojiMemoryGame
var body: some View {
ScrollView {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 65))]) {
ForEach(viewModel.cards) {
card in CardView(card: card).aspectRatio(2 / 3, contentMode: .fit)
.onTapGesture {
viewModel.choose(card)
}
}
}
}
.foregroundColor(/*@START_MENU_TOKEN@*/ .red/*@END_MENU_TOKEN@*/)
.padding(.horizontal)
}
}
struct CardView: View {
let card: MemoryGame<String>.Card
var body: some View {
ZStack {
let shape = RoundedRectangle(cornerRadius: 20.0)
if card.isFaceUp {
shape.fill().foregroundColor(.white)
shape.strokeBorder(lineWidth: 3)
Text(card.content).font(.largeTitle)
} else if card.isMatched {
shape.opacity(0)
} else {
shape.fill()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
let game = EmojiMemoryGame()
ContentView(viewModel: game)
.preferredColorScheme(.dark)
ContentView(viewModel: game)
.preferredColorScheme(.light)
}
}