3

我正在尝试为 UILabel 设置动画(淡入/淡出),并且正在使用以下代码:

float newAlpha = 0.0;

//TODO:Check if the previous animation has finished

if(answer.alpha==0.0) {
    newAlpha = 1.0;
} else if(answer.alpha==1.0) {
    newAlpha = 0.0;
}
[UIView animateWithDuration:1.0 animations:^{
    answer.alpha = newAlpha;
}];

TODO 注释在哪里,我想检查上一个动画是否完成,如果没有,则退出该方法。有没有办法做到这一点?

4

3 回答 3

3

更新#1:

你的班级需要一个变量:

BOOL _animationFinished;

然后您可以对动画使用以下方式:

float newAlpha = 0.0;

//TODO:Check if the previous animation has finished
if (_animationFinished == false) return;

if(answer.alpha==0.0) {
    newAlpha = 1.0;
} else if(answer.alpha==1.0) {
    newAlpha = 0.0;
}

[UIView animateWithDuration:1.0f animations:^{ answer.alpha = newAlpha; _animationFinished = false; } completion:^(BOOL finished){ _animationFinished = true; }];

它必须是工作。


原来的

在这种情况下,我总是检查动画的主题,如下所示:

float newAlpha = 0.0;

//TODO:Check if the previous animation has finished
if (answer.alpha > 0.f || answer.alpha < 1.f) return; // it is always good enough for me
// ...or with AND it will cause the same effect:
// if (answer.alpha > 0.f && answer.alpha < 1.f) return;

if(answer.alpha==0.0) {
    newAlpha = 1.0;
} else if(answer.alpha==1.0) {
    newAlpha = 0.0;
}
[UIView animateWithDuration:1.0 animations:^{
    answer.alpha = newAlpha;
}];
于 2012-07-25T10:24:45.080 回答
2

如果您使用的是 UIView 那么

[UIView setAnimationDidStopSelector:@selector(animationfinished)];
-(void) animationfinished
{
      animationFinished = YES;
}
于 2012-07-25T10:06:16.283 回答
1

使用animateWithDuration:animations:completion:方法来执行您的“上一个动画”,并在完成处理程序中设置一个标志以指示它是否已完成。然后,在你有 TODO 注释的地方检查相同的标志。

编辑:下面的示例

-(void) animation1 {
    // assume that alpha was 0 and we want the view to appear
    [UIView animateWithDuration:1.0 animations:^{
        answer.alpha = 1.0;
    } completion:^(BOOL finished){
        fristAnimationFinished = finished;
    }];
}

-(void) animation2 {
    float newAlpha = 0.0;

    if (!firstAnimationFinished)
        return;

    if(answer.alpha==0.0) {
        newAlpha = 1.0;
    } else if(answer.alpha==1.0) {
        newAlpha = 0.0;
    }
    [UIView animateWithDuration:1.0 animations:^{
        answer.alpha = newAlpha;
    }];
}
于 2012-07-25T10:06:22.977 回答