简体   繁体   English

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

[英]When creating an animation to cause a UIImageView to go up and down (almost float) infinitely, how do I stop a pause at the end of the animation?

Here's the code I'm using to take a UIImageView and make it float up and down. 这是我用来获取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) {

    }];

However, it comes out looking like this with this delay after the animation ends and before it restarts. 但是,在动画结束之后和重新开始之前,延迟一段时间后看起来像这样。

How do I make it fluid? 我如何使其流畅?

I'm not sure what effect you're going for exactly, but I think this gives you something like your code does without the delay. 我不确定您到底要达到什么效果,但是我认为这可以为您提供类似于代码的效果而不会延迟。 I usually do this by animating a constraint, rather than using transforms. 我通常通过设置约束动画来实现此目的,而不是使用变换。 In this example, I've made an IBOutlet (topCon) to the constraint to the top of the view: 在此示例中,我对视图顶部的约束做了一个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];
    }];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM