100 lines
2.1 KiB
Swift
100 lines
2.1 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@*/)
|
|
Spacer()
|
|
HStack{
|
|
remove
|
|
Spacer()
|
|
add
|
|
}
|
|
.padding(.horizontal)
|
|
.font(.largeTitle)
|
|
}
|
|
.padding(.horizontal)
|
|
}
|
|
|
|
var remove: some View {
|
|
Button(action: {
|
|
if emojiCount > 1{
|
|
emojiCount -= 1
|
|
}
|
|
}, label: {
|
|
Image(systemName: "minus.circle")
|
|
})
|
|
}
|
|
var add: some View {
|
|
Button(action: {
|
|
if emojiCount < emojis.count {
|
|
emojiCount += 1
|
|
}
|
|
}, label: {
|
|
Image(systemName: "plus.circle")
|
|
})
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|