UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 99 Challenge - quite tricky to flip the card back

Forums > 100 Days of Swift

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

3      

Solved it by pulling out the creation of the view from the button's action and placing it at the birthplace of the button...It wasn't smart putting it there :D

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.