Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 328
satvikb Avatar asked Apr 21 '16 22:04

satvikb


2 Answers

Swift 3:

text.transform = CGAffineTransform.identity
UIView.animate(withDuration: 0.25, animations: {
   self.text.transform = CGAffineTransform(scaleX: scale, y: scale)
})
like image 158
Danut Pralea Avatar answered Nov 04 '22 20:11

Danut Pralea


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

self.text.transform = CGAffineTransformMakeScale(scale, scale)
like image 45
rmaddy Avatar answered Nov 04 '22 22:11

rmaddy