Hi guys,
I've spent a day trying to complete the challenge but I'm stuck at flipping the card back part...I've generated buttons with imageView for the cards and they mostly work except for this part: if I flip two cards that don't match they flip back but, if I select the first card again it's not flipping back for some reason. I think it's because I'm misusing the viewWithTag method to keep track of the previous button views. Could I get a hint? Below is my code, beware, it's a mess... Thank you!
@objc func cardTapped(_ sender: UIButton) {
guard let title = sender.titleLabel?.text else { return }
//test flipping
let someView = createflippedLabel(text: title)
someView.tag = 1984
sender.addSubview(someView)
someView.isHidden = true
if !firstCardFlipped {
assignFirstCard(card: title)
previousButton = sender
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
UIView.transition(from: sender.imageView!, to: someView, duration: 0.3, options: [.transitionFlipFromRight, .showHideTransitionViews], completion: nil)
}
firstCardFlipped = true
} else {
//this works with no problem
if matches[firstCard!] == title || matches[title] == firstCard {
print("matched!")
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
UIView.transition(from: sender.imageView!, to: someView, duration: 0.3, options: [.transitionFlipFromRight, .showHideTransitionViews], completion: nil)
}
sender.removeFromSuperview()
previousButton?.removeFromSuperview()
previousButton = nil
firstCardFlipped = false
firstCard = ""
} else {
//this works but with problem :D
guard let lastButton = previousButton else { return }
print("Not matched!")
print("\(lastButton.titleLabel?.text ?? "") \(title)")
firstCardFlipped = false
firstCard = ""
//flip the current card and flip back
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
UIView.transition(from: sender.imageView!, to: someView, duration: 0.3, options: [.transitionFlipFromRight, .showHideTransitionViews], completion: nil)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
UIView.transition(from: someView, to: sender.imageView!, duration: 0.3, options: [.transitionFlipFromRight, .showHideTransitionViews], completion: nil)
}
//flip the previous card back to imageView(the biohazard thing...)
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
UIView.transition(from: lastButton.viewWithTag(1984)!, to: lastButton.imageView!, duration: 0.3, options: [.transitionFlipFromRight, .showHideTransitionViews], completion: nil)
}
print(previousButton?.titleLabel?.text)
previousButton = nil
}
}
}
https://gifyu.com/image/UkZ4