From 08dc4700a35c0fee5724aacb26e8812b870a6c79 Mon Sep 17 00:00:00 2001 From: Ching Date: Sun, 12 Feb 2023 23:19:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(view,=20contstants):=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=20card=20=E4=B8=AD=E5=AD=97=E4=BD=93=E5=A4=A7=E5=B0=8F?= =?UTF-8?q?=E4=B8=BA=E8=87=AA=E9=80=82=E5=BA=94=EF=BC=9B=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=20constants?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改 card 中字体大小为自适应;增加 constants Signed-off-by: Ching --- Memorize.xcodeproj/project.pbxproj | 4 ++++ Memorize/Constans.swift | 20 +++++++++++++++++++ Memorize/EmojiMemoryGameView.swift | 32 +++++++++++++++++++----------- 3 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 Memorize/Constans.swift diff --git a/Memorize.xcodeproj/project.pbxproj b/Memorize.xcodeproj/project.pbxproj index 873e942..ad020ae 100644 --- a/Memorize.xcodeproj/project.pbxproj +++ b/Memorize.xcodeproj/project.pbxproj @@ -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 = ""; }; 245099F22998EAD6000CE9DA /* MemoryGame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryGame.swift; sourceTree = ""; }; 245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiMemoryGame.swift; sourceTree = ""; }; + 24E748FB29993781009B5FE8 /* Constans.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constans.swift; sourceTree = ""; }; /* 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 */, ); diff --git a/Memorize/Constans.swift b/Memorize/Constans.swift new file mode 100644 index 0000000..6958ef9 --- /dev/null +++ b/Memorize/Constans.swift @@ -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 + } +} diff --git a/Memorize/EmojiMemoryGameView.swift b/Memorize/EmojiMemoryGameView.swift index 937fc5d..990d3de 100644 --- a/Memorize/EmojiMemoryGameView.swift +++ b/Memorize/EmojiMemoryGameView.swift @@ -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,20 +27,28 @@ struct EmojiMemoryGameView: View { struct CardView: View { let card: EmojiMemoryGame.Card + + var body: some View { - ZStack { - let shape = RoundedRectangle(cornerRadius: 20.0) - if card.isFaceUp && !card.isMatched { - shape.fill().foregroundColor(.white) - shape.strokeBorder(lineWidth: 3) - Text(card.content).font(.largeTitle) - } else if card.isMatched { - shape.opacity(0) - } else { - shape.fill() + 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) + 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 {