cs193p-memorize/Memorize/EmojiMemoryGameView.swift
Ching 32ea11b525 feat(view): 增加 aspectvgrid view
增加 aspectvgrid view

Signed-off-by: Ching <loooching@gmail.com>
2023-02-15 17:34:24 +08:00

68 lines
2.2 KiB
Swift

//
// EmojiMemoryGameView.swift
// Memorize
//
// Created by ching on 2023/2/12.
//
import SwiftUI
struct EmojiMemoryGameView: View {
@ObservedObject var game: EmojiMemoryGame
var body: some View {
// ScrollView {
// LazyVGrid(columns: [GridItem(.adaptive(minimum: const.DrawingConstants.gridWidth))]) {
// ForEach(game.cards) {
// card in CardView(card: card).aspectRatio(const.DrawingConstants.gridAspectRatio, contentMode: .fit)
// .onTapGesture {
// game.choose(card)
// }
// }
// }
// }
// .foregroundColor(/*@START_MENU_TOKEN@*/ .red/*@END_MENU_TOKEN@*/)
// .padding(.horizontal)
AspectVGrid(items: game.cards, aspectRatio: const.DrawingConstants.gridAspectRatio, content: {
card in CardView(card: card).aspectRatio(const.DrawingConstants.gridAspectRatio, contentMode: .fit)
.onTapGesture {
game.choose(card)
}
})
}
struct CardView: View {
let card: EmojiMemoryGame.Card
var body: some View {
GeometryReader { geometry in
ZStack {
let shape = RoundedRectangle(cornerRadius: const.DrawingConstants.cornerRadius)
if card.isFaceUp && !card.isMatched {
shape.fill().foregroundColor(.white)
shape.strokeBorder(lineWidth: const.DrawingConstants.lineWidth)
Text(card.content).font(font(in: geometry.size))
} else if card.isMatched {
shape.opacity(const.DrawingConstants.matchedCardOpacity)
} else {
shape.fill()
}
}
}
}
private func font(in size: CGSize) -> Font {
Font.system(size: min(size.width, size.height) * const.DrawingConstants.fontScale)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
let game = EmojiMemoryGame()
EmojiMemoryGameView(game: game)
.preferredColorScheme(.dark)
EmojiMemoryGameView(game: game)
.preferredColorScheme(.light)
}
}