Zog
Zog

Reputation: 1143

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.

Upvotes: 0

Views: 178

Answers (3)

Sweeper
Sweeper

Reputation: 272760

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
}

Upvotes: 1

SilentLupin
SilentLupin

Reputation: 658

If you want to keep your gesture recognizer, then you can have an array of formats and an index held by your view controller that starts at 0 on init. On each tap, increment the index (with modulo so that it will go back to 0), then set the format of your countdown to the one in the array at the current index.

Upvotes: 0

Traveler
Traveler

Reputation: 191

I would simplify this solution as possible.

You can put a button and on tap just handle it in the code (you don't need gesture recogniser obviously) and change text according to state. Like by default you have minutes, on tap you just change state to hours and so on.

how to change state? I would just use enum and like state += 1

Upvotes: 1

Related Questions