feat(view, contstants): 修改 card 中字体大小为自适应;增加 constants

修改 card 中字体大小为自适应;增加 constants

Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
Ching 2023-02-12 23:19:35 +08:00
parent 449fa1b8e9
commit 08dc4700a3
3 changed files with 44 additions and 12 deletions

View File

@ -13,6 +13,7 @@
240EDC462998A3BA00A46AC9 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 240EDC452998A3BA00A46AC9 /* Preview Assets.xcassets */; };
245099F32998EAD6000CE9DA /* MemoryGame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 245099F22998EAD6000CE9DA /* MemoryGame.swift */; };
245099F52998EC71000CE9DA /* EmojiMemoryGame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */; };
24E748FC29993782009B5FE8 /* Constans.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24E748FB29993781009B5FE8 /* Constans.swift */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@ -23,6 +24,7 @@
240EDC452998A3BA00A46AC9 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
245099F22998EAD6000CE9DA /* MemoryGame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryGame.swift; sourceTree = "<group>"; };
245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiMemoryGame.swift; sourceTree = "<group>"; };
24E748FB29993781009B5FE8 /* Constans.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constans.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -57,6 +59,7 @@
children = (
240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */,
240EDC402998A3B900A46AC9 /* EmojiMemoryGameView.swift */,
24E748FB29993781009B5FE8 /* Constans.swift */,
245099F22998EAD6000CE9DA /* MemoryGame.swift */,
245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */,
240EDC422998A3BA00A46AC9 /* Assets.xcassets */,
@ -145,6 +148,7 @@
files = (
245099F32998EAD6000CE9DA /* MemoryGame.swift in Sources */,
240EDC412998A3B900A46AC9 /* EmojiMemoryGameView.swift in Sources */,
24E748FC29993782009B5FE8 /* Constans.swift in Sources */,
240EDC3F2998A3B900A46AC9 /* MemorizeApp.swift in Sources */,
245099F52998EC71000CE9DA /* EmojiMemoryGame.swift in Sources */,
);

20
Memorize/Constans.swift Normal file
View File

@ -0,0 +1,20 @@
//
// Constans.swift
// Memorize
//
// Created by ching on 2023/2/12.
//
import Foundation
import SwiftUI
struct const {
enum DrawingConstants {
static let cornerRadius: CGFloat = 20
static let lineWidth: CGFloat = 3
static let fontScale: CGFloat = 0.8
static let gridWidth: CGFloat = 80
static let gridAspectRatio: CGFloat = 2 / 3
static let matchedCardOpacity: Double = 0
}
}

View File

@ -11,9 +11,9 @@ struct EmojiMemoryGameView: View {
@ObservedObject var game: EmojiMemoryGame
var body: some View {
ScrollView {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 65))]) {
LazyVGrid(columns: [GridItem(.adaptive(minimum: const.DrawingConstants.gridWidth))]) {
ForEach(game.cards) {
card in CardView(card: card).aspectRatio(2 / 3, contentMode: .fit)
card in CardView(card: card).aspectRatio(const.DrawingConstants.gridAspectRatio, contentMode: .fit)
.onTapGesture {
game.choose(card)
}
@ -27,15 +27,18 @@ struct EmojiMemoryGameView: View {
struct CardView: View {
let card: EmojiMemoryGame.Card
var body: some View {
GeometryReader { geometry in
ZStack {
let shape = RoundedRectangle(cornerRadius: 20.0)
let shape = RoundedRectangle(cornerRadius: const.DrawingConstants.cornerRadius)
if card.isFaceUp && !card.isMatched {
shape.fill().foregroundColor(.white)
shape.strokeBorder(lineWidth: 3)
Text(card.content).font(.largeTitle)
shape.strokeBorder(lineWidth: const.DrawingConstants.lineWidth)
Text(card.content).font(font(in: geometry.size))
} else if card.isMatched {
shape.opacity(0)
shape.opacity(const.DrawingConstants.matchedCardOpacity)
} else {
shape.fill()
}
@ -43,6 +46,11 @@ struct CardView: View {
}
}
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()