-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryGameView.swift
66 lines (58 loc) · 1.66 KB
/
MemoryGameView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// ContentView.swift
// Memorize
//
// Created by Kynji Kyle Niño A. Pepito on 9/19/23.
//
import SwiftUI
struct MemoryGameView: View {
@ObservedObject var viewModel: EmojiMemoryGame
var body: some View {
VStack {
ScrollView {
cards
.animation(.default, value: viewModel.cards)
}
Button("SHUFFLE") {
viewModel.shuffle()
}
}.padding()
}
var cards: some View {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 85), spacing: 0)], spacing: 0) {
ForEach(viewModel.cards) { card in
CardView(card)
.aspectRatio(2/3, contentMode: .fit)
.padding(4)
}
}.foregroundColor(Color.brown)
}
}
struct CardView: View {
let card: MemoryGame<String>.Card
init(_ card: MemoryGame<String>.Card) {
self.card = card
}
@State var isUp: Bool = true
var body: some View {
ZStack {
let base = RoundedRectangle(cornerRadius: 12)
Group {
base.foregroundColor(Color.white)
base.strokeBorder(lineWidth: 2)
Text(card.face)
.font(.system(size: 200))
.minimumScaleFactor(0.01)
.aspectRatio(1, contentMode: .fit)
}
.opacity(card.isFaceUp ? 1 : 0)
base.fill()
.opacity(card.isFaceUp ? 0 : 1)
}
}
}
struct MemoryGameView_Previews: PreviewProvider {
static var previews: some View {
MemoryGameView(viewModel: EmojiMemoryGame())
}
}