iOS how to animate view scaling from the center of the view?

889 Views Asked by At

I'm using an affine transform within a repeating/reversing animation to create a "pulsing" effect. However, I noticed that the button gets shifted right when the animation begins. The anchor point for the button is at (0.5,0.5):

   [UIView animateWithDuration:0.5
                          delay:0
                        options:UIViewAnimationOptionRepeat|
     UIViewAnimationOptionAutoreverse|
     UIViewAnimationOptionAllowUserInteraction|
     UIViewAnimationCurveEaseInOut
                     animations:^{
                         self.recordButton.transform = CGAffineTransformMakeScale(1.2, 1.2);
                     } completion:^(BOOL finished) {

                     }];

It appears that when the transform is applied, the origin of the button's frame stays at the same spot.

How can I animate scale transform so the center of the button remains in the same spot once the transform is applied?

1

There are 1 best solutions below

0
On

I usually play with the frame and CGRect utility functions

[UIView animateWithDuration:0.5
                      delay:0
                    options:UIViewAnimationOptionRepeat|
 UIViewAnimationOptionAutoreverse|
 UIViewAnimationOptionAllowUserInteraction|
 UIViewAnimationCurveEaseInOut
                 animations:^{
                     self.recordButton.frame = CGRectInset(self.recordButton.frame, -1.2, -1.2);
                 } completion:^(BOOL finished) {

                 }];