sborra sborra - 1 year ago 111
Swift Question

Swift CGAffineTransformScale to a scale, not by a scale

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 Source

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

self.text.transform = CGAffineTransformMakeScale(scale, scale)