iPad: Move UIView with animation, then move it back

Posted by Joel on Stack Overflow See other posts from Stack Overflow or by Joel
Published on 2010-05-24T17:56:23Z Indexed on 2010/05/24 18:01 UTC
Read the original article Hit count: 665

Filed under:
|
|
|
|

I have the following code in a UIView subclass that will move it off the screen with an animation:

float currentX = xposition; //variables that store where the UIView is located
float currentY = yposition;

float targetX = -5.0f - self.width;
float targetY = -5.0f - self.height;

moveX = targetX - currentX; //stored as an instance variable so I can hang on to it and move it back
moveY = targetY - currentY;

[UIView beginAnimations:@"UIBase Hide" context:nil];
[UIView setAnimationDuration:duration]; 
self.transform = CGAffineTransformMakeTranslation(moveX,moveY);
[UIView commitAnimations];

It works just great. I've changed targetX/Y to 0 to see if it does indeed move to the specified point, and it does.

The problem is when I try and move the view back to where it was originally:

moveX = -moveX;
moveY = -moveY;

[UIView beginAnimations:@"UIBase Show" context:nil];
[UIView setAnimationDuration:duration];
self.transform = CGAffineTransformMakeTranslation(moveX,moveY);
[UIView commitAnimations];

This puts the UIView about 3x as far as it should. So the view usually gets put off the right side of the screen (depending on where it is).

Is there something with the CGAffineTransforMakeTranslation function that I should know? I read the documentation, and from what I can tell, it should be doing what I'm expecting it to do.

Anyone have a suggestion of how to fix this? Thanks.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about iphone-sdk