2022-04-14 翻面之前

ContentView

import SwiftUI


struct ContentView: View {
    var viewModel: EmojiMemoryGame
    
    var body: some View {
        VStack {
            ScrollView {
                LazyVGrid(columns: [GridItem(.adaptive(minimum: 65))]){
                    ForEach(viewModel.cards) { card in
                        CardView(card: card).aspectRatio(2/3, contentMode: .fit)
                    }
                }
            }
            .foregroundColor(.red)
        
        }
        .padding(.horizontal)
        
    }
    
}

struct CardView: View {
    var card: MemoryGame.Card
    var body: some View {
        ZStack {
            let shape = RoundedRectangle(cornerRadius: 20)
            if card.isFaceUp {
                shape.fill().foregroundColor(.white)
                shape.strokeBorder(lineWidth: 3)
                Text(card.content).font(.largeTitle)
                
            } else {
                shape.fill()
            }
        }
        
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        let game = EmojiMemoryGame()
        ContentView(viewModel: game)
            .preferredColorScheme(/*@START_MENU_TOKEN@*/.dark/*@END_MENU_TOKEN@*/)
        ContentView(viewModel: game)
            .preferredColorScheme(.light)
    }
}

MemoryGame

import Foundation

struct MemoryGame {
    var cards: Array
    
    func choose(_ card: Card) {
        
    }
    
    init(numberOfPairsOfCards: Int, createCardContent: (Int) -> CardContent) {
        cards = []
        for pairIndex in 0..

EmojiMemoryGame

import SwiftUI


class EmojiMemoryGame {
    static var emojis = ["", "", "⛱", "", "", "", "", "⏱", "☎️", "","","", "✈️"]
    
    static func makeMemoryGame() -> MemoryGame {
        MemoryGame(numberOfPairsOfCards: 4){pairIndex in
             emojis[pairIndex]
        }
    }
    var model: MemoryGame = makeMemoryGame()
    
    var cards: Array.Card> {
        model.cards
    }
}

你可能感兴趣的:(2022-04-14 翻面之前)