n.evermind
n.evermind

Reputation: 12004

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:CGRectOffset([pageShadowView frame], -100, 0)];        
    [UIView commitAnimations];

Upvotes: 2

Views: 2462

Answers (2)

Rayfleck
Rayfleck

Reputation: 12106

 [pageShadowView setFrame:CGRectMake(xStart, yStart, xWidth, yHeight)]; // x,y in superview coordinates

Upvotes: 3

Bastian
Bastian

Reputation: 10433

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)
                        );

Upvotes: 5

Related Questions