0

I'm trying to animate a label to change its text with different words at different keyframes, but it just skips to the last keyframe ("Good Night") without displaying the previous ones. It does print them out so I guess there is something wrong with either the duration or startTime but I can't figure out what it could be..

func changeText() {

    let options = UIView.KeyframeAnimationOptions.calculationModeLinear

    UIView.animateKeyframes(withDuration: 10, delay: 0, options: options, animations: {

        UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.25, animations: {
            self.label.text = "Good Morning"
            print("Morning")
        })

        UIView.addKeyframe(withRelativeStartTime: 0.25, relativeDuration: 0.25, animations: {
            self.label.text = "Good Afternoon"
            print("Afternoon")
        })

        UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.25, animations: {
            self.label.text = "Good Evening"
            print("Evening")
        })
        UIView.addKeyframe(withRelativeStartTime: 0.75, relativeDuration: 0.25, animations: {
            self.label.text = "Good Night"
            print("Night")
        })

    }, completion: nil)
}

@IBAction func didPressBtn(_ sender: Any) {
    changeText()
}
Henrik
  • 69
  • 1
  • 2
  • 4

2 Answers2

0

This should achieve what your are after?

 func changeText() {

 label.text = "Good Morning"
 print("Morning")
 sleep(2.5)

 label.text = "Good Afternoon"
 print("Morning")
 sleep(2.5)

 label.text = "Good Evening"
 print("Morning")
 sleep(2.5)

 label.text = "Good Night"
 print("Morning")
 sleep(2.5)

 }
wades
  • 250
  • 2
  • 10
  • Apologies, my solution doesn't factor in the animations, if you are set on the animations maybe try using `sleep(2.5)` to slow things down – wades Apr 17 '19 at 13:58
  • Thanks, will give it a try. I'm actually having the label in a view that gets animated in (like a countdown "3".."2".."1".."GO") for a start sequence. It works fine when I make a view for each "number", but the problems come when I reuse the same view and only want to change the text/label. Is there a difference between DispatchQueue and sleep? – Henrik Apr 23 '19 at 08:30
  • sleep will essentially pause the function for the amount of seconds in brackets. for example `sleep(2)` will pause for 2 seconds then continue – wades Apr 24 '19 at 04:07
0

I do not believe changing the text of a UILabel is directly animateable in UIView.animate.

A possible solution to how it looks like you want it to work would be to create four different UILabels, and animate between them as needed.

There are 3rd party libraries available, however! https://github.com/lexrus/LTMorphingLabel (found via https://github.com/ameizi/awesome-ios-animation )

solenoid
  • 954
  • 1
  • 9
  • 20
  • Bummer. Yeah that's the solution I gone with so far. It would have been nice to just change the text and not having to have all the extra code for the extra labels... – Henrik Apr 23 '19 at 08:35
  • I updated my answer since this is actually something I have wanted to do, and found 3rd party libs that can do it) – solenoid Apr 23 '19 at 15:35