为什么这个动画的移动低于它开始的位置?

时间:2012-07-31 16:42:19

标签: iphone ios animation

我有一个简单的动画,可以上下移动UIView。

当它正确向上移动时 - 当它向下移动时它会向下移动太远。

为什么不向下移动相同的距离?

- (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];
}

我发现它向下移动了两倍 - 我不明白.....

所以当我写这篇文章时 - 无论出于何种原因,它都能正常工作......

   if (up)
        movementDistance = 120;
    else {
        movementDistance =60;
    }

2 个答案:

答案 0 :(得分:2)

您正在将视图移动到-120,这将离开屏幕。您需要像这样引用inputView的frame.origin.y:

- (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];
}

这应该会给你正确的结果

编辑: 您的视图向下移动两倍的原因是因为例如 - 假设你的原始y轴为0.如果你向上移动它然后它会变为120,因为这就是你的运动距离,然后一旦你想要它向下移动它只是直接移动到-120。它传递0(你原来的y轴)。

答案 1 :(得分:1)

嗯,我不知道为什么会那样。通常我会使用他们的中心为视图制作动画,我觉得框架可以被修剪等等并导致问题。试试这个

self.myInputView.center = CGPointMake (self.myInputView.center.x,self.myInputView.center.y - movement);

我只是在这里猜测但请告诉我它是否有效。