Repeat Shrinking An Animation

I am trying to have 2 animations one that increases the label's size and makes it return to its original form but I can't repeat the shrinking like I can increasing the size. How can I shrink it it on loop?

Code Block
func animateLabels() {
    UIView.animate(withDuration: 2, delay: 0, options: .repeat, animations: {
      //1.5 times it's normal size
      self.proLabel.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
    }){ (finished) in
      UIView.animate(withDuration: 2, delay: 2, options: .repeat, animations: {
        self.proLabel
          .transform = CGAffineTransform.identity
      })
    }
  }

Answered by Claude31 in 645748022
Then, when do you stop ?

You can either set a very large value for repeat or simply :

Code Block
func animateLabels() {
UIView.animate(withDuration: 2, delay: 0, options: [.repeat, .autoreverse], animations: {
self.proLabel.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
})
}


You don't even need the completion handler
Code Block
{ (finished) in
self.proLabel.transform = CGAffineTransform.identity
}

if you have an infinite repeat (it will never be called)
What do you want ? Do see the size decrease smoothly when finished ?

To get it, I changed animateLabels as follows:

Code Block
func animateLabels() {
UIView.animate(withDuration: 2, delay: 0, options: [ .autoreverse], animations: {
// 1.5 times it's normal size
self.label.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
}){ (finished) in
// UIView.animate(withDuration: 0, delay: 0, options: [], animations: {
self.label.transform = CGAffineTransform.identity
// })
}
}


The thing is I want the decrease to repeat and be smooth.
Did you try my proposed change ?

If it works, don't forget to close the thread.

If not, could you explain what is not OK ?
Do you want repeat ? How many times ?

You could then change func as follows:
Code Block
func animateLabels() {
UIView.animate(withDuration: 2, delay: 0, options: [.autoreverse], animations: {
UIView.setAnimationRepeatCount(3.0) // For 3 times repetition
self.proLabel.transform = CGAffineTransform(scaleX: 1.5, y: 1.5) // 1.5 times its normal size
}){ (finished) in
self.proLabel.transform = CGAffineTransform.identity
}
}

I always try a potential solution and it does not work because I want it to repeat indefinitely.
Accepted Answer
Then, when do you stop ?

You can either set a very large value for repeat or simply :

Code Block
func animateLabels() {
UIView.animate(withDuration: 2, delay: 0, options: [.repeat, .autoreverse], animations: {
self.proLabel.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
})
}


You don't even need the completion handler
Code Block
{ (finished) in
self.proLabel.transform = CGAffineTransform.identity
}

if you have an infinite repeat (it will never be called)
Thank you for the help I just needed the options: [.repeat, .autoreverse] and I got rid of the next handler for the continuous repeat.
Repeat Shrinking An Animation
 
 
Q