2

I am trying to do a little animation which change a label frame along with changing the font size. I have seen that animating directly the font size is not possible. So I have tried to use CGAffineTransform.

Here is what I have tried so far :

    UIView.animateWithDuration(1, animations: {
        if self.topConstraint.active {
            self.initialTopConstraint.active = false
            self.initialLeadingConstraint.active = false

            self.label.transform = CGAffineTransformMakeScale(0.5, 0.5)

            self.finalTopConstraint.active = true
            self.finalLeadingConstraint.active = true

        }

        else {
            self.initialTopConstraint.active = true
            self.initialLeftConstraint.active = true

            self.label.transform = CGAffineTransformMakeScale(1.5, 1.5)

            self.finalTopConstraint.active = false
            self.finalLeftConstraint.active = false

        }
        self.view.layoutIfNeeded()
    })

In fact, this works, but I am using auto layout, and at the end of the animation, the constraints are not satisfied at all. The final constraints constants are 8, and when I print the label frame at the end of the animation, the label is around 13 points from the left and around 16 points from the top.

Initial frame :

enter image description here

Final frame with constraints respected but no font size changes made :

enter image description here

Final frame with constraints not respected and font size changes made :

enter image description here

Is there anyway to combine properly Auto layout and CGAffineTransform ?

meteorSD
  • 339
  • 3
  • 16
  • Are there any differences with this similar question? http://stackoverflow.com/questions/14494566/animating-uilabel-font-size-change?rq=1 – Kevin Jul 02 '16 at 15:49
  • I would give different suggestion, you can add another view which of same size and add constraints to that view. Add this label as subview to that view with center aligned and size constraints. Hide container view. Now you don't have to make any changes to constraints. Make your animations. – Rajath Shetty K Jul 02 '16 at 15:52
  • The difference with that question is that I am using CGAffineTransform and Auto layout, but i am basically trying to do the same thing. I am not sure i got you Rajath, maybe my question is not clear enough ? – meteorSD Jul 02 '16 at 16:11
  • I also need help with this. did you ever figure it out? – Travis Delly Oct 12 '17 at 01:55
  • Please, look at this answer: https://stackoverflow.com/a/44259220/6643923 . – nik Apr 19 '18 at 07:59

0 Answers0