权限控制 private private(set)
改名 command+左键
代码清理 typealias Card = MemoryGame<String>.Card
类型推断 1,保留函数 -> 的类型 其他做推断 2,保留变量里的类型 下面做推断
计算属性
MemorizeApp.swift -------------
import SwiftUI
@main
struct MemorizeApp: App {
private let game = EmojiMemoryGame()
var body: some Scene {
WindowGroup {
EmojiMemoryGameView(game: game)
}
}
}
EmojiMemoryGameView.swift------------------------
import SwiftUI
struct EmojiMemoryGameView: View {
@ObservedObject var game: EmojiMemoryGame
var body: some View {
ScrollView {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 65))]){
ForEach(game.cards) { card in
CardView(card).aspectRatio(2/3, contentMode: .fit)
.onTapGesture {
game.choose(card)
}
}
}
}
.foregroundColor(.red)
.padding(.horizontal)
}
}
struct CardView: View {
private let card: EmojiMemoryGame.Card
//这里如果有init 则可以把card设置为private,通过init访问,CardView(card: card) 则成CardView(card)
// private var card: EmojiMemoryGame.Card
init(_ card: EmojiMemoryGame.Card) {
self.card = card //后参前变,my card = 传过来的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 if card.isMatched {
shape.opacity(0)
}
else {
shape.fill()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
let game = EmojiMemoryGame()
EmojiMemoryGameView(game: game)
.preferredColorScheme(/*@START_MENU_TOKEN@*/.dark/*@END_MENU_TOKEN@*/)
EmojiMemoryGameView(game: game)
.preferredColorScheme(.light)
}
}
MemoryGame.swift -------------------
import Foundation
struct MemoryGame<CardContent> where CardContent: Equatable {
private(set) var cards: Array<Card>
private var indexOfTheOneAndOnlyFaceUpCard: Int?
mutating func choose(_ card: Card) {
if let chosenIndex = cards.firstIndex(where: { $0.id == card.id }),
!cards[chosenIndex].isMatched,
!cards[chosenIndex].isFaceUp
{
if let potentialIndex = indexOfTheOneAndOnlyFaceUpCard {
if cards[chosenIndex].content == cards[potentialIndex].content {
cards[chosenIndex].isMatched = true
cards[potentialIndex].isMatched = true
}
indexOfTheOneAndOnlyFaceUpCard = nil
} else {
for index in cards.indices {
cards[index].isFaceUp = false
}
indexOfTheOneAndOnlyFaceUpCard = chosenIndex
}
cards[chosenIndex].isFaceUp.toggle()
}
}
init(numberOfPairOfCards: Int, createCardContent: (Int) -> CardContent) {
cards = []
for pairIndex in 0..<numberOfPairOfCards {
let content = createCardContent(pairIndex)
cards.append(Card(content: content, id: pairIndex*2))
cards.append(Card(content: content, id: pairIndex*2+1))
}
}
struct Card:Identifiable {
var isFaceUp = false
var isMatched = false
let content: CardContent
let id: Int
}
}
EmojiMemoryGame.swift -------------------
import SwiftUI
class EmojiMemoryGame: ObservableObject {
typealias Card = MemoryGame<String>.Card
private static var emojis = ["🪓", "🚙", "🚗", "⛱", "🎡", "🪝", "🗿", "🛖", "⏱", "☎️", "🎰","🚜","🛴", "✈️"]
private static func createMemoryGame() -> MemoryGame<String> {
MemoryGame<String>(numberOfPairOfCards: 4){ pairIndex in emojis[pairIndex]}
}
@Published private var model = createMemoryGame()
var cards: Array<Card> {
return model.cards
}
func choose(_ card: Card) {
return model.choose(card)
}
}
网友评论