Reputation: 11053
I have a simple animation that moves an UIView up and down.
While it moves up correctly - when it moves down it moves much too far down.
Why doesn't it move the same distance down as up?
- (void) animateInputViewUp: (BOOL) up
{
const int movementDistance = 120;
const float movementDuration = 0.4f;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.myInputView.frame = CGRectOffset(self.myInputView.frame, 0, movement);
[UIView commitAnimations];
}
I found out that it moves down twice as much as up - I don't understand.....
so when I write this - it works correctly - for whatever reason...
if (up)
movementDistance = 120;
else {
movementDistance =60;
}
Upvotes: 0
Views: 52
Reputation: 9098
You are moving the view to -120, which would be off the screen. You need to reference the inputView's frame.origin.y like so:
- (void) animateInputViewUp: (BOOL) up
{
const int movementDistance = 120;
const float movementDuration = 0.4f;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.myInputView.frame = CGRectOffset(self.myInputView.frame, 0, self.myInputView.frame.origin.y + movement);
[UIView commitAnimations];
}
That should give you the correct result
Edit: The reason why your view is moving twice as much down is because for instance -- let's say your original y-axis is 0. If you move it up then it goes to 120 because that's what your movementDistance is, then once you want it go back down it just moves straight to -120. Which passes 0 (your original y-axis).
Upvotes: 2
Reputation: 14851
Hmmm, I'm not sure why is it like that. Usually I animate views using their centers, I feel like frames can get clipped etc. and cause problems. Try this
self.myInputView.center = CGPointMake (self.myInputView.center.x,self.myInputView.center.y - movement);
I'm just guessing here but let me know if it works.
Upvotes: 1