开发者

iPad: Move UIView with animation, then move it back

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.


Assigning to a transform will cause the old one to disappear. To return to the original position, assign the identity transform to it.

self.transform = CGAffineTransformIdentity;

(Or better, just change the .frame instead of changing the .transform.)


UIView transform isn't additive; I suggest that to move it back you instead do:

self.transform = CGAffineTransformIdentity;

The docs say (about updating the frame):

"Warning: If this property is not the identity transform, the value of the frame property is undefined and therefore should be ignored."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜