Question

I have a UIButton laid out using storyboard. The button just contains an image. When the button is clicked, I want to animate the size of the button - decrease in size and then bring it back to the original size again.

I used the following code -

[UIView animateWithDuration:2.0 animations:^{
    _favButton.transform = CGAffineTransformMakeScale(0.5, 0.5);
}completion:^(BOOL finished) {
    [UIView animateWithDuration:2.0 animations:^{
          _favButton.transform = CGAffineTransformMakeScale(1, 1);
    }];
}];

This code moves my button on the screen which I do not want. I want the center of the button to be fixed and the size be animated.

I have not used any Top Constraint in the storyboard for the button. How can I rectify this behaviour?

Was it helpful?

Solution

If you have auto layout turned on, you would need to turn it off.

But it doesn't seem to your problem here as per your description.

I would do the following to re-adjust to the center as it scales:

CGPoint cP = _favButton.center;

[UIView animateWithDuration:2.0 animations:^
{
    _favButton.transform = CGAffineTransformMakeScale(0.5, 0.5);
    _favButton.layer.position = cp;
}
completion:^(BOOL finished) 
{
    [UIView animateWithDuration:2.0 animations:^
    {
        _favButton.transform = CGAffineTransformMakeScale(1, 1);
        _favButton.layer.position = cp;
    }];
}];

Hope this helps.

OTHER TIPS

SWIFT 3

button.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top