Search code examples
iosswiftlabelswipe

Swipe to change label content in swift


I'm a beginner in coding and I have this question:

Example, I have 4 different labels with their own messages.

Label1 - you should read the rules
Label2 - congratulations you read the rules
Label3 - now you can join us
Label4 - welcome aboard

I want to be able to do something like this:

Label1 is shown, swipe right > label2 appears > swipe right > label3 appears > swipe right > label4 appears
I also want to be able to swipe left and return to the previous label.

Ex.
label1>label2>label3


Solution

  • You could keep each label in an array, create a counter and in the swipe method increase or decrease de counter value to select the corresponding label with the array index of the counter.

    Like so:

    Swipe right:

    counter+=1 currentLabel = labelsArray[counter] as! UILabel

    Swipe left:

    counter-=1 currentLabel = labelsArray[counter] as! UILabel

    But why not use a collectionview instead? You can make it paged and it'll be something that the users are more used to.