c0dec0de
c0dec0de

Reputation: 373

UIImageView Transform Scale

I have a UIButton that when pressed, will make a UIImageView scale slightly larger and then back to its normal size. It's working, but I'm running into a problem where the image eventually keeps getting slightly smaller the more you press the button.

How can I change the code so that the image doesn't get smaller as you press the button? Here's the code I have to scale the image slightly larger and then back to normal:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.2];
myImage.transform = CGAffineTransformScale(myImage.transform, 1.03, 1.03);
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.2];             
myImage.transform = CGAffineTransformScale(myImage.transform, 0.97, 0.97);
[UIView setAnimationDelegate:self];
[UIView commitAnimations];

Thanks for any help.

Upvotes: 6

Views: 15142

Answers (3)

iWheelBuy
iWheelBuy

Reputation: 5679

Have you tried saving first transform?

And then instead of using CGAffineTransformScale to shrink your UIImageView you just use your saved transform?

        CGAffineTransform firstTransform = myImage.transform;

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration: 0.2];
        myImage.transform = CGAffineTransformScale(myImage.transform, 1.03, 1.03);
        [UIView setAnimationDelegate:self];
        [UIView commitAnimations];
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration: 0.2];
        myImage.transform = firstTransform;
        [UIView setAnimationDelegate:self];
        [UIView commitAnimations];

Upvotes: 3

Cesar Alaniz
Cesar Alaniz

Reputation: 41

You are suffering from a limitation in floating point arithmetic and cumulative matrix operations. You will never be able to represent 3/100, only approximate it with values like, 0.03, 0.033, and 0.0333.

For your problem it would be better to set the second transform to the CGAffineTransformIdentity

Upvotes: 1

Ole Begemann
Ole Begemann

Reputation: 135578

It's math. Scaling something by 1.03 * 0.97 results in scaling factor of 0.9991 and not 1.0. Either use 1.0/1.03 as your second scaling factor or just set myImage.transform to the identity transform (assuming you are not applying other transformations to that view).

Upvotes: 6

Related Questions