From a296e999c0f6046eaff4b6ed2970af24e0f56ad1 Mon Sep 17 00:00:00 2001 From: Ching Date: Wed, 15 Feb 2023 23:31:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(view):=20=E5=A2=9E=E5=8A=A0=20cardify=20vi?= =?UTF-8?q?ew?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加 cardify view Signed-off-by: Ching --- Memorize.xcodeproj/project.pbxproj | 4 ++++ Memorize/Cardify.swift | 30 ++++++++++++++++++++++++++++++ Memorize/Constans.swift | 2 +- Memorize/EmojiMemoryGameView.swift | 21 ++++++--------------- 4 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 Memorize/Cardify.swift diff --git a/Memorize.xcodeproj/project.pbxproj b/Memorize.xcodeproj/project.pbxproj index d5766fb..8d782b8 100644 --- a/Memorize.xcodeproj/project.pbxproj +++ b/Memorize.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 240EBF09299CFCE900429C8A /* Cardify.swift in Sources */ = {isa = PBXBuildFile; fileRef = 240EBF08299CFCE900429C8A /* Cardify.swift */; }; 240EDC3F2998A3B900A46AC9 /* MemorizeApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */; }; 240EDC412998A3B900A46AC9 /* EmojiMemoryGameView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 240EDC402998A3B900A46AC9 /* EmojiMemoryGameView.swift */; }; 240EDC432998A3BA00A46AC9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 240EDC422998A3BA00A46AC9 /* Assets.xcassets */; }; @@ -19,6 +20,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 240EBF08299CFCE900429C8A /* Cardify.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Cardify.swift; sourceTree = ""; }; 240EDC3B2998A3B900A46AC9 /* Memorize.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Memorize.app; sourceTree = BUILT_PRODUCTS_DIR; }; 240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemorizeApp.swift; sourceTree = ""; }; 240EDC402998A3B900A46AC9 /* EmojiMemoryGameView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiMemoryGameView.swift; sourceTree = ""; }; @@ -66,6 +68,7 @@ 24FE51AE299CED6F00798617 /* Pie.swift */, 24E748FD299944F4009B5FE8 /* AspectVGrid.swift */, 24E748FB29993781009B5FE8 /* Constans.swift */, + 240EBF08299CFCE900429C8A /* Cardify.swift */, 245099F22998EAD6000CE9DA /* MemoryGame.swift */, 245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */, 240EDC422998A3BA00A46AC9 /* Assets.xcassets */, @@ -154,6 +157,7 @@ files = ( 245099F32998EAD6000CE9DA /* MemoryGame.swift in Sources */, 240EDC412998A3B900A46AC9 /* EmojiMemoryGameView.swift in Sources */, + 240EBF09299CFCE900429C8A /* Cardify.swift in Sources */, 24E748FE299944F4009B5FE8 /* AspectVGrid.swift in Sources */, 24E748FC29993782009B5FE8 /* Constans.swift in Sources */, 24FE51AF299CED6F00798617 /* Pie.swift in Sources */, diff --git a/Memorize/Cardify.swift b/Memorize/Cardify.swift new file mode 100644 index 0000000..f69fb0b --- /dev/null +++ b/Memorize/Cardify.swift @@ -0,0 +1,30 @@ +// +// Cardify.swift +// Memorize +// +// Created by ching on 2023/2/15. +// + +import SwiftUI + +struct Cardify: ViewModifier { + var isFaceUp: Bool + func body(content: Content) -> some View { + ZStack { + let shape = RoundedRectangle(cornerRadius: const.DrawingConstants.cornerRadius) + if isFaceUp { + shape.fill().foregroundColor(.white) + shape.strokeBorder(lineWidth: const.DrawingConstants.lineWidth) + content + } else { + shape.fill() + } + } + } +} + +extension View { + func cadify(isFaceUp: Bool) -> some View { + self.modifier(Cardify(isFaceUp: isFaceUp)) + } +} diff --git a/Memorize/Constans.swift b/Memorize/Constans.swift index 594d84a..aca16ac 100644 --- a/Memorize/Constans.swift +++ b/Memorize/Constans.swift @@ -12,7 +12,7 @@ struct const { enum DrawingConstants { static let cornerRadius: CGFloat = 10 static let lineWidth: CGFloat = 3 - static let fontScale: CGFloat = 0.7 + static let fontScale: CGFloat = 0.6 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 a97410c..ed438b3 100644 --- a/Memorize/EmojiMemoryGameView.swift +++ b/Memorize/EmojiMemoryGameView.swift @@ -34,21 +34,12 @@ struct CardView: View { var body: some View { 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) - Pie(startAngle: Angle(degrees: 270), - endAngle: Angle(degrees: 380)) - .padding(const.DrawingConstants.piePadding) - .opacity(const.DrawingConstants.pieOpacity) - Text(card.content).font(font(in: geometry.size)) - } else if card.isMatched { - shape.opacity(const.DrawingConstants.matchedCardOpacity) - } else { - shape.fill() - } - } + Pie(startAngle: Angle(degrees: 270), + endAngle: Angle(degrees: 380)) + .padding(const.DrawingConstants.piePadding) + .opacity(const.DrawingConstants.pieOpacity) + Text(card.content).font(font(in: geometry.size)) + }.cadify(isFaceUp: card.isFaceUp) } }