Search code examples
iosswifttap

Swift - How to show different text upon taps in sequence


So I have a countdown app and have the different formats of countdown. This includes days minutes hours seconds until a certain date. I want to make only one variable appear at a time.

So it would go:days TAP minutes TAP hours TAP seconds

Then once seconds is taped it goes back to the start. so it would go seconds TAP days and keep repeating.

I have tried using gesture recogniser but I am catastrophically stuck, please could you help.


Solution

  • I'll give you a straightforward solution.

    First, create an enum that represents the thing you're showing at the moment:

    enum CountDownMode {
        case Days, Minutes, Hours, Seconds
    }
    

    Then, declare a variable like this in the controller:

    var currentMode = CountDownMode.Days
    

    And actually, you don't need to use gesture recognizers for recognizing such simple touches. Just override touchesBegan:

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
    }
    

    In touchesBegan, using the value of currentMode, you can easily figure out what mode is next and change the text accordingly:

    switch currentMode {
    case .Days:
        // change text here
        currentMode = .Minutes
    case .Minutes:
        // change text here
        currentMode = .Hours
    case .Hours:
        // change text here
        currentMode = .Seconds
    case .Seconds:
        // change text here
        currentMode = .Days
    }