iOS查找视图是否为动画

时间:2012-07-25 09:59:49

标签: objective-c ios animation

我正在尝试动画(淡入/淡出)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评论的位置,我想检查上一个动画是否已经完成,如果没有,请退出该方法。有没有办法做到这一点?

3 个答案:

答案 0 :(得分: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; }];

一定是工作。


<强> ORIGINAL

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

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

答案 1 :(得分:2)

如果您正在使用UIView,那么

[UIView setAnimationDidStopSelector:@selector(animationfinished)];
-(void) animationfinished
{
      animationFinished = YES;
}

答案 2 :(得分: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;
    }];
}