UIImageView Conversion Scale

I have a UIButton that, when clicked, will make the UIImageView scale a little larger, and then return to its normal size. It works, but I ran into a problem when the image gradually becomes a little smaller, the more you press the button.

How can I change the code so that the image does not shrink when I click the button? Here is the code that I have to scale the image a bit more, and then return 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.

+6
ios objective-c iphone
source share
3 answers

This is math. Scaling something with 1.03 * 0.97 results in a scaling factor of 0.9991 , not 1.0 . Either use 1.0/1.03 as your second scaling factor, or simply set myImage.transform to an identity transformation (assuming you are not applying other transformations to this view).

+6
source share

Did you try to save the first conversion?

And instead of using CGAffineTransformScale to compress your UIImageView , are you just using a 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]; 
+3
source share

You suffer from limitations in floating point arithmetic and cumulative matrix operations. You can never imagine 3/100; approximate it only with the values 0.03 , 0.033 and 0.0333 .

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

+1
source share

All Articles