7

Let's say I scale a UILabel using a CGAffineTransformScale like so:

let scale = 0.5
text = UILabel(frame: CGRectMake(100, 100, 100, 100))
text.text = "Test"

UIView.animateWithDuration(2.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {
    self.text.transform = CGAffineTransformScale(self.text.transform, scale, scale)
}, completion: {(value : Bool) in
    print("Animation finished")
})

This works great when I want to scale the UILabel by half. But if I were to call this same code again, it would end up with a scale of 0.25, as it scales again by half.

Would it be possible to use the CGAffineTransformScale to always scale to a size of half the original UILabel frame, instead of a scaling it cumulatively?

satvikb
  • 319
  • 1
  • 3
  • 17

3 Answers3

16

Swift 3:

text.transform = CGAffineTransform.identity
UIView.animate(withDuration: 0.25, animations: {
   self.text.transform = CGAffineTransform(scaleX: scale, y: scale)
})
Dani Pralea
  • 4,545
  • 2
  • 31
  • 49
6

You are scaling the existing transform. Just create a new transform:

self.text.transform = CGAffineTransformMakeScale(scale, scale)
rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

Swift version:

self.text.transform = CGAffineTransform.init(scaleX: scaleFactorX, y: scaleFactorY)
Henadzi Rabkin
  • 6,834
  • 3
  • 32
  • 39