feat(view): 增加 aspectvgrid view
增加 aspectvgrid view Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
parent
08dc4700a3
commit
32ea11b525
@ -14,6 +14,7 @@
|
|||||||
245099F32998EAD6000CE9DA /* MemoryGame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 245099F22998EAD6000CE9DA /* MemoryGame.swift */; };
|
245099F32998EAD6000CE9DA /* MemoryGame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 245099F22998EAD6000CE9DA /* MemoryGame.swift */; };
|
||||||
245099F52998EC71000CE9DA /* EmojiMemoryGame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */; };
|
245099F52998EC71000CE9DA /* EmojiMemoryGame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */; };
|
||||||
24E748FC29993782009B5FE8 /* Constans.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24E748FB29993781009B5FE8 /* Constans.swift */; };
|
24E748FC29993782009B5FE8 /* Constans.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24E748FB29993781009B5FE8 /* Constans.swift */; };
|
||||||
|
24E748FE299944F4009B5FE8 /* AspectVGrid.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24E748FD299944F4009B5FE8 /* AspectVGrid.swift */; };
|
||||||
/* End PBXBuildFile section */
|
/* End PBXBuildFile section */
|
||||||
|
|
||||||
/* Begin PBXFileReference section */
|
/* Begin PBXFileReference section */
|
||||||
@ -25,6 +26,7 @@
|
|||||||
245099F22998EAD6000CE9DA /* MemoryGame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryGame.swift; 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>"; };
|
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>"; };
|
24E748FB29993781009B5FE8 /* Constans.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constans.swift; sourceTree = "<group>"; };
|
||||||
|
24E748FD299944F4009B5FE8 /* AspectVGrid.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AspectVGrid.swift; sourceTree = "<group>"; };
|
||||||
/* End PBXFileReference section */
|
/* End PBXFileReference section */
|
||||||
|
|
||||||
/* Begin PBXFrameworksBuildPhase section */
|
/* Begin PBXFrameworksBuildPhase section */
|
||||||
@ -59,6 +61,7 @@
|
|||||||
children = (
|
children = (
|
||||||
240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */,
|
240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */,
|
||||||
240EDC402998A3B900A46AC9 /* EmojiMemoryGameView.swift */,
|
240EDC402998A3B900A46AC9 /* EmojiMemoryGameView.swift */,
|
||||||
|
24E748FD299944F4009B5FE8 /* AspectVGrid.swift */,
|
||||||
24E748FB29993781009B5FE8 /* Constans.swift */,
|
24E748FB29993781009B5FE8 /* Constans.swift */,
|
||||||
245099F22998EAD6000CE9DA /* MemoryGame.swift */,
|
245099F22998EAD6000CE9DA /* MemoryGame.swift */,
|
||||||
245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */,
|
245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */,
|
||||||
@ -148,6 +151,7 @@
|
|||||||
files = (
|
files = (
|
||||||
245099F32998EAD6000CE9DA /* MemoryGame.swift in Sources */,
|
245099F32998EAD6000CE9DA /* MemoryGame.swift in Sources */,
|
||||||
240EDC412998A3B900A46AC9 /* EmojiMemoryGameView.swift in Sources */,
|
240EDC412998A3B900A46AC9 /* EmojiMemoryGameView.swift in Sources */,
|
||||||
|
24E748FE299944F4009B5FE8 /* AspectVGrid.swift in Sources */,
|
||||||
24E748FC29993782009B5FE8 /* Constans.swift in Sources */,
|
24E748FC29993782009B5FE8 /* Constans.swift in Sources */,
|
||||||
240EDC3F2998A3B900A46AC9 /* MemorizeApp.swift in Sources */,
|
240EDC3F2998A3B900A46AC9 /* MemorizeApp.swift in Sources */,
|
||||||
245099F52998EC71000CE9DA /* EmojiMemoryGame.swift in Sources */,
|
245099F52998EC71000CE9DA /* EmojiMemoryGame.swift in Sources */,
|
||||||
|
|||||||
48
Memorize/AspectVGrid.swift
Normal file
48
Memorize/AspectVGrid.swift
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
//
|
||||||
|
// AspectVGrid.swift
|
||||||
|
// Memorize
|
||||||
|
//
|
||||||
|
// Created by ching on 2023/2/13.
|
||||||
|
//
|
||||||
|
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
struct AspectVGrid<Item, ItemView>: View where ItemView: View, Item: Identifiable {
|
||||||
|
var items: [Item]
|
||||||
|
var aspectRatio: CGFloat
|
||||||
|
var content: (Item) -> ItemView
|
||||||
|
var body: some View {
|
||||||
|
let width: CGFloat = 100
|
||||||
|
LazyVGrid(columns: [GridItem(.adaptive(minimum: width))]) {
|
||||||
|
ForEach(items) {
|
||||||
|
item in content(item).aspectRatio(aspectRatio, contentMode: .fit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func widthThatFits(itemCount: Int, in size: CGSize, itemAspectRatio: CGFloat) -> CGFloat {
|
||||||
|
var columCount = 1
|
||||||
|
var rowCount = itemCount
|
||||||
|
|
||||||
|
repeat {
|
||||||
|
let itemWidth = size.width / CGFloat(columCount)
|
||||||
|
let itemHeight = itemWidth / itemAspectRatio
|
||||||
|
if CGFloat(rowCount) * itemHeight < size.height {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
columCount += 1
|
||||||
|
rowCount = (itemCount + columCount - 1) / columCount
|
||||||
|
} while columCount < itemCount
|
||||||
|
|
||||||
|
if columCount > itemCount {
|
||||||
|
columCount = itemCount
|
||||||
|
}
|
||||||
|
return floor(size.width / CGFloat(columCount))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// struct AspectVGrid_Previews: PreviewProvider {
|
||||||
|
// static var previews: some View {
|
||||||
|
// AspectVGrid()
|
||||||
|
// }
|
||||||
|
// }
|
||||||
@ -10,20 +10,25 @@ import SwiftUI
|
|||||||
struct EmojiMemoryGameView: View {
|
struct EmojiMemoryGameView: View {
|
||||||
@ObservedObject var game: EmojiMemoryGame
|
@ObservedObject var game: EmojiMemoryGame
|
||||||
var body: some View {
|
var body: some View {
|
||||||
ScrollView {
|
// ScrollView {
|
||||||
LazyVGrid(columns: [GridItem(.adaptive(minimum: const.DrawingConstants.gridWidth))]) {
|
// LazyVGrid(columns: [GridItem(.adaptive(minimum: const.DrawingConstants.gridWidth))]) {
|
||||||
ForEach(game.cards) {
|
// ForEach(game.cards) {
|
||||||
card in CardView(card: card).aspectRatio(const.DrawingConstants.gridAspectRatio, contentMode: .fit)
|
// card in CardView(card: card).aspectRatio(const.DrawingConstants.gridAspectRatio, contentMode: .fit)
|
||||||
.onTapGesture {
|
// .onTapGesture {
|
||||||
game.choose(card)
|
// game.choose(card)
|
||||||
}
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// .foregroundColor(/*@START_MENU_TOKEN@*/ .red/*@END_MENU_TOKEN@*/)
|
||||||
|
// .padding(.horizontal)
|
||||||
|
AspectVGrid(items: game.cards, aspectRatio: const.DrawingConstants.gridAspectRatio, content: {
|
||||||
|
card in CardView(card: card).aspectRatio(const.DrawingConstants.gridAspectRatio, contentMode: .fit)
|
||||||
|
.onTapGesture {
|
||||||
|
game.choose(card)
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
|
||||||
.foregroundColor(/*@START_MENU_TOKEN@*/ .red/*@END_MENU_TOKEN@*/)
|
|
||||||
.padding(.horizontal)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
struct CardView: View {
|
struct CardView: View {
|
||||||
let card: EmojiMemoryGame.Card
|
let card: EmojiMemoryGame.Card
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user