Search code examples
iosswiftuilabeluitapgesturerecognizer

How to detect if UI Label was tapped?


I'm trying to make a choose-your-own adventure game that changes the text of two labels (the user choices) depending on which label the user taps. I figured I would just do a very nested if-else statement rather than bother with trying to implement a binary tree. I know how to attach the gesture recognizer to a label (I think):

let tapped1 = UITapGestureRecognizer(target: self, action: #selector(VCGame.usrChose1))  
choice1.isUserInteractionEnabled = true  
choice1.addGestureRecognizer(tapped1)  

let tapped2 = UITapGestureRecognizer(target: self, action: #selector(VCGame.usrChose2))  
choice2.isUserInteractionEnabled = true  
choice2.addGestureRecognizer(tapped2)  

and I can define what to do when the label is touched in the usrChose1 and usrChose2 functions, however, those functions only work once: the first time the function is chosen and my game has more than just one choice. From there, the labels will just do the same thing if the user touches them.

How would I go about having a condition inside the if-else statement that evaluates to true or false if label1 or label2 is tapped?

Here's the code for usrChoice1 and usrChoice2, for clarification

func usrChose1(sender : UITapGestureRecognizer) {
    print("tap 1 working")
    choice1.text = "choice1.1"
    choice2.text = "choice1.2"
}

func usrChose2(sender : UITapGestureRecognizer) {
    print("tap2 working")
    choice1.text = "update2.1";
    choice2.text = "update2.2"
}

Below image shows my requirement :

here's a chart i made to help illustrate the logic


Solution

  • According to your requirement, I have tried the following:

    I have made a dummy project with two labels inside a view controller

    ViewController.swift

    import UIKit
    
    class ViewController: UIViewController {
    
            @IBOutlet weak var choice1Label: UILabel!
    
            @IBOutlet weak var choiceLabel2: UILabel!
    
            var tapStart: Bool = false
    
            var levelType1: Level?
            var levelType2: Level?
    
            override func viewDidLoad() {
                    super.viewDidLoad()
    
    
    
    
    
                    let tapped1 = UITapGestureRecognizer(target: self, action: #selector(usrChose1))
                    choice1Label.isUserInteractionEnabled = true
                    choice1Label.addGestureRecognizer(tapped1)
    
                    let tapped2 = UITapGestureRecognizer(target: self, action: #selector(usrChose2))
                    choiceLabel2.isUserInteractionEnabled = true
                    choiceLabel2.addGestureRecognizer(tapped2)
    
                    setup()
    
            }
    
            var currentLevel1: Level?
            var currentLevel2: Level?
    
    
            func setup() {
                    let lb2Child1Child1 = Level(text: "2.1.1", subLevels: nil)
                    let lb2Child1Child2 = Level(text: "2.1.2", subLevels: nil)
    
    
                    let lb1Child1Child1 = Level(text: "1.1.1", subLevels: nil)
                    let lb1Child1Child2 = Level(text: "1.1.2", subLevels: nil)
    
                    let lb1Child2Child1 = Level(text: "1.2.1", subLevels: nil)
                    let lb1Child2Child2 = Level(text: "1.2.2", subLevels: nil)
    
                    let lb1Child1 = Level(text: "1.1", subLevels: [lb1Child1Child1, lb1Child1Child2])
                    let lb1Child2 = Level(text: "1.2", subLevels: [lb1Child2Child1, lb1Child2Child2])
    
    
                    let lb2Child1 = Level(text: "2.1", subLevels: [lb2Child1Child1, lb2Child1Child2])
                    let lb2Child2 = Level(text: "2.2", subLevels: nil)
    
    
                    levelType1 = Level(text: "1", subLevels: [lb1Child1, lb1Child2])
                    levelType2 = Level(text: "2", subLevels: [lb2Child1, lb2Child2])
                    choice1Label.text = levelType1!.text ?? ""
                    choiceLabel2.text = levelType2!.text ?? ""
    
    
            }
    
            func usrChose1(sender : UITapGestureRecognizer) {
    
                    if !tapStart {
    
                            currentLevel1 = levelType1
    
                            tapStart = true
                    }
    
                    if let subLevelsArray = currentLevel1?.subLevels {
    
                            print(subLevelsArray[0].text ?? "")
                            print(subLevelsArray[1].text ?? "")
    
                            choice1Label.text = subLevelsArray[0].text ?? ""
                            choiceLabel2.text = subLevelsArray[1].text ?? ""
    
                            currentLevel1 = subLevelsArray[0]
                            currentLevel2 = subLevelsArray[1]
                    }
    
    
            }
    
            func usrChose2(sender : UITapGestureRecognizer) {
                    //print("tap2 working")
                   // choice1Label.text = "update2.1";
                    //choiceLabel2.text = "update2.2"
    
    
    
                    if !tapStart {
    
    
                            currentLevel2 = levelType2
    
                            tapStart = true
                    }
    
                    if let subLevelsArray = currentLevel2?.subLevels {
    
                            print(subLevelsArray[0].text ?? "")
                            print(subLevelsArray[1].text ?? "")
                            choice1Label.text = subLevelsArray[0].text ?? ""
                            choiceLabel2.text = subLevelsArray[1].text ?? ""
                            currentLevel1 = subLevelsArray[0]
                            currentLevel2 = subLevelsArray[1]
    
                    }
            }
    
    }
    

    I have made a class named Level to represent a single level and each level contains sublevels

    Level.swift

    import UIKit
    
    class Level {
            var text: String?
    
            var subLevels: [Level]?
    
    
            init(text: String, subLevels: [Level]?) {
                    self.text = text
                    self.subLevels = subLevels ?? nil
    
            }
    
    
    
    
    }