Swift CGAffineTransformScale to a scale, not by a scale
Asked Answered
S

3

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?

Swayder answered 21/4, 2016 at 22:2 Comment(0)
I
16

Swift 3:

text.transform = CGAffineTransform.identity
UIView.animate(withDuration: 0.25, animations: {
   self.text.transform = CGAffineTransform(scaleX: scale, y: scale)
})
Insatiate answered 4/11, 2016 at 23:24 Comment(0)
D
6

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

self.text.transform = CGAffineTransformMakeScale(scale, scale)
Delineation answered 21/4, 2016 at 22:4 Comment(1)
or CGAffineTransformScale(CGAffineTransformIdentity, scale, scale)Beore
C
0

Swift version:

self.text.transform = CGAffineTransform.init(scaleX: scaleFactorX, y: scaleFactorY)
Cripple answered 9/4, 2021 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.