cs193p-memorize/Memorize/ContentView.swift
Ching 87b6465e68 feat(view, viewmodel): 新增model 和对应的 viewmodel
新增model 和对应的 viewmodel

Signed-off-by: Ching <loooching@gmail.com>
2023-02-12 18:28:14 +08:00

64 lines
1.6 KiB
Swift

//
// ContentView.swift
// Memorize
//
// Created by ching on 2023/2/12.
//
import SwiftUI
struct ContentView: View {
var emojis = ["🚌", "🚙", "🚗", "🚕", "🏎", "🚎", "🚓"]
@State var emojiCount = 2
var body: some View {
VStack {
ScrollView {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 65))]) {
ForEach(emojis[0 ..< emojiCount], id: \.self) {
emoji in CardView(content: emoji).aspectRatio(2 / 3, contentMode: .fit)
}
}
}
.foregroundColor(/*@START_MENU_TOKEN@*/ .red/*@END_MENU_TOKEN@*/)
.padding(.horizontal)
.font(.largeTitle)
}
.padding(.horizontal)
}
}
struct CardView: View {
@State var isFaceUp: Bool = true
var content: String
var body: some View {
ZStack {
let shape = RoundedRectangle(cornerRadius: 20.0)
if isFaceUp {
shape
.fill()
.foregroundColor(.white)
shape
.strokeBorder(lineWidth: 3)
Text(content)
.font(.largeTitle)
}
else {
shape
.fill()
}
}
.onTapGesture {
isFaceUp = !isFaceUp
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.preferredColorScheme(.dark)
ContentView()
.preferredColorScheme(.light)
}
}