cs193p-memorize/Memorize/EmojiMemoryGameView.swift
Ching 08dc4700a3 feat(view, contstants): 修改 card 中字体大小为自适应;增加 constants
修改 card 中字体大小为自适应;增加 constants

Signed-off-by: Ching <loooching@gmail.com>
2023-02-12 23:19:35 +08:00

63 lines
1.9 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)
}
}
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)
}
}