在创建动画以使UIImageView无限上下(几乎浮动)时,如何在动画结束时停止暂停?

时间:2013-11-01 04:00:30

标签: ios objective-c uiview

以下是我用来取UIImageView并使其上下浮动的代码。

    [UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionRepeat animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.25 animations:^{
            self.slider.transform = CGAffineTransformMakeTranslation(0, -5.0);
        }];
        [UIView addKeyframeWithRelativeStartTime:0.25 relativeDuration:0.5 animations:^{
            self.slider.transform = CGAffineTransformMakeTranslation(0, 5.0);
        }];
        [UIView addKeyframeWithRelativeStartTime:0.75 relativeDuration:0.25 animations:^{
            self.slider.transform = CGAffineTransformMakeTranslation(0, 0.0);
        }];
    } completion:^(BOOL finished) {

    }];

然而,在动画结束之后和重新开始之前,它看起来像这样延迟。

如何让它变得流畅?

1 个答案:

答案 0 :(得分:1)

我不确定你究竟会有什么影响,但我认为这会给你一些像你的代码那样没有延迟的东西。我通常通过动画约束来实现这一点,而不是使用变换。在这个例子中,我已经将约束的IBOutlet(topCon)放到了视图的顶部:

-(IBAction)floatView:(id)sender {
    static int i= 1;
    static float duration = .25;
    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.topCon.constant = self.topCon.constant - (5 * i);
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        i = (i == 1 || i == 2)? -2 : 2;
        duration = 0.5;
        [self floatView:self];
    }];
}