Animation of UIView: how to reset the position
I have a very basic beginner's question regarding the position of UIViews that I animate.
If I call this
[pageShadowView setFrame:CGRectOffset([pageShadowView frame], 100, 0)];
and then animate this
[pageShadowView setFrame:CGRectOffset([pageShadowView frame], -100, 0)];
I will basically move my UIView from the right side of the screen to the left. However, if I would specify -150 in the second line of code, and if I were to repeat these animations a couple of times, my object would gradually move more and more to the left.
This is a very fundamental question, I'm afraid, but is there a way to specify fixed X coordinates instead of defining relative positions? I.e. suppose I always wanted to move the object from x = 200 to x = 100.
My entire method looks like this:
[pageShadowView setFrame:CGRectOffset([pageShadowView frame], 100, 0)];
[UIView beginAnimations:@"shadowMove" context:nil]; // Begin animation
[UIView setAnimationDuration:2.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[pageShadowView setFrame:CG开发者_运维知识库RectOffset([pageShadowView frame], -100, 0)];
[UIView commitAnimations];
You can set any rect with setFrame... just create one with CGRectMake() with absolute positions.
e.g.
pageShadowView.frame = CGRectMake(
x,
y,
CGRectGetWidth(pageShadowView.frame),
CGRectGetHeight(pageShadowView.frame)
);
[pageShadowView setFrame:CGRectMake(xStart, yStart, xWidth, yHeight)]; // x,y in superview coordinates
精彩评论