iOS:+ [UIView animateWithDuration:]不使用持续时间

时间:2012-03-06 22:48:25

标签: ios uiviewanimation

我正在尝试动画“堕落”到iPad屏幕底部的视图,最后稍微反弹。

弹跳效果很好。我的问题是“掉落”的动画根本没有像它应该的那样工作。

我希望动画是线性的。 (我知道;“下降”不是线性的,但我认为在我的情况下它很好。)它似乎很容易安心。更大的问题是我希望它在我指定的持续时间内完成。事实并非如此。如果距离很小,例如100像素,则持续时间计算为0.1秒,并且似乎需要大约0.1秒。但是如果距离很大,比如1000像素,那么持续时间应该是1秒,但它要长得多:大约3秒。

有什么想法吗?这是代码:

NSTimeInterval duration = (distanceToTravel / (float)1000);
NSLog(@"distance = %f, duration = %f", distanceToTravel, duration);
[UIView animateWithDuration:duration delay:0 options:(UIViewAnimationCurveLinear) animations:^ {
        wpcvc.view.center = wpcvc.startingDragLocation;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 delay:0 options:( UIViewAnimationCurveEaseOut) animations:^ {
            wpcvc.view.center = CGPointMake(wpcvc.startingDragLocation.x, wpcvc.startingDragLocation.y - 2);
        } completion:^(BOOL finished2) {
            [UIView animateWithDuration:0.2 delay:0 options:(UIViewAnimationCurveEaseIn) animations:^ {
                wpcvc.view.center = wpcvc.startingDragLocation;
            } completion:^(BOOL finished3) {
            }];
        }];
    }];

感谢任何帮助!

4 个答案:

答案 0 :(得分:7)

你不应该为UIView.animateWithDuration使用UIViewAnimationCurveLinear选项

对于线性动画,你必须使用UIViewAnimationOptions的选项,在你的情况下是这个UIViewAnimationOptionCurveLinear(请注意选项 - 差异):

[UIView animateWithDuration:duration delay:0 options:(UIViewAnimationOptionCurveLinear) animations:^ {
    wpcvc.view.center = wpcvc.startingDragLocation;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.2 delay:0 options:( UIViewAnimationOptionCurveEaseOut) animations:^ {
        wpcvc.view.center = CGPointMake(wpcvc.startingDragLocation.x, wpcvc.startingDragLocation.y - 2);
    } completion:^(BOOL finished2) {
        [UIView animateWithDuration:0.2 delay:0 options:(UIViewAnimationOptionCurveEaseIn) animations:^ {
            wpcvc.view.center = wpcvc.startingDragLocation;
        } completion:^(BOOL finished3) {
        }];
    }];
}];

答案 1 :(得分:3)

为了确保,你有没有关闭慢动画?模拟器具有慢动画调试设置(我偶尔偶然切换)。

enter image description here

答案 2 :(得分:1)

你是否在已经有隐式动画的地方这样做,比如viewWillAppear:,viewDidAppear:等等?如果是这样,可能会搞乱第一个动画的设置,因为其中的所有内容都在动画块中。

答案 3 :(得分:0)

' wpcvc'没有超级视图? (或超级超级视图呢?)

[(superview) addSubview:wpcvc];
[UIView animateWithDuration:...

预先将目标视图添加到某个超级视图中。

相关问题