最近跟著油管一博主學用swift寫翻牌消除游戲,但是遇到一個bug,就是我總是要點這張牌沒反應,只有點了下一張牌上一張牌才會翻過來。啊,到底是一股什么力量讓它這么任性吶!!!米娜桑,求幫看代碼。。。紅色部分是點選翻牌的代碼。
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
var model = CardModel()
var cardArray = [Card]()
override func viewDidLoad() {
super.viewDidLoad()
cardArray = model.getCards()
collectionView.delegate = self
collectionView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//dispose of any resource that can be recreated.
}
//這里是刷出新的item
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cardArray.count
}
//這里是設定collectionviewcell的操作
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//get a CardCollectionViewCell object
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cardCell", for: indexPath) as! CardCollectionViewCell
//get the card that the collection view is trying to display
let card = cardArray[indexPath.row]
//set that card for the cell
cell.setCard(card)
return cell
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! CardCollectionViewCell
let card = cardArray[indexPath.row]
if card.isFlipped == false{
cell.flip()
card.isFlipped = true
}else {
cell.flipback()
card.isFlipped = false
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/67363.html
標籤:iOS
