70 lines
2.2 KiB
Swift
70 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 {
|
|
AspectVGrid(items: game.cards, aspectRatio: const.DrawingConstants.gridAspectRatio) {
|
|
card in
|
|
if card.isMatched && !card.isFaceUp {
|
|
Rectangle().opacity(0)
|
|
} else {
|
|
CardView(card: card)
|
|
.padding(const.DrawingConstants.gridPadding)
|
|
.onTapGesture {
|
|
game.choose(card)
|
|
}
|
|
}
|
|
}
|
|
.foregroundColor(.red)
|
|
.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)
|
|
Pie(startAngle: Angle(degrees: 270),
|
|
endAngle: Angle(degrees: 380))
|
|
.padding(const.DrawingConstants.piePadding)
|
|
.opacity(const.DrawingConstants.pieOpacity)
|
|
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()
|
|
game.choose(game.cards.first!)
|
|
return EmojiMemoryGameView(game: game)
|
|
// .preferredColorScheme(.dark)
|
|
// EmojiMemoryGameView(game: game)
|
|
.preferredColorScheme(.light)
|
|
}
|
|
}
|