Swift CGAffineTransformScale to a scale, not by a scale

satvikb picture satvikb · Apr 22, 2016 · Viewed 9.2k times · Source

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?

Answer

Danut Pralea picture Danut Pralea · Nov 5, 2016

Swift 3:

text.transform = CGAffineTransform.identity
UIView.animate(withDuration: 0.25, animations: {
   self.text.transform = CGAffineTransform(scaleX: scale, y: scale)
})