Animation 在for循环内部的UIVIEW中链接动画

Animation 在for循环内部的UIVIEW中链接动画,animation,core-animation,uilabel,sequence,uiviewanimation,Animation,Core Animation,Uilabel,Sequence,Uiviewanimation,我在动画方面有一些问题。这就是我要做的。 首先,我有一个for循环,例如,我在其中制作一些uilabel的动画 label1.frame = CGRectMake(xBoard, 5, textWidth, term1Label.frame.size.height); xBoard = xBoard + textWidth; label1.textColor = term1Label.textColor; label1.font = [UIFont italicS

我在动画方面有一些问题。这就是我要做的。 首先,我有一个for循环,例如,我在其中制作一些uilabel的动画

    label1.frame = CGRectMake(xBoard, 5, textWidth, term1Label.frame.size.height);
    xBoard = xBoard + textWidth;
    label1.textColor = term1Label.textColor;
    label1.font = [UIFont italicSystemFontOfSize:textHeight];
    label1.textAlignment = term1Label.textAlignment;
    label1.text = label1String;
    label1.alpha = 0;
    label1.backgroundColor = [UIColor clearColor];
    [boardView addSubview:label1];

    [UIView animateWithDuration:2.0*time
                          delay:timeDelay
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                           label1.alpha = 1;
                   } completion:^(BOOL finished){
                             }
                 ];

    timeDelay = timeDelay + 2.0*time;
一些标签出现,一些消失,一些改变颜色。在我进行第二次循环之前,一切都正常。然后代码的第一部分就出现了,改变颜色的动画仍然在工作,但是第一个循环中的所有标签都是可见的,并且没有动画。第二个循环的动画效果很好


我试图用CABasicAnimations替换动画[UIView animateWithDuration:…],并将它们保存在一个NSMutableArray with queue中,但没有成功。也许我的知识还太短。欢迎任何帮助。谢谢
         NSArray *animation1 = [[NSArray alloc]initWithObjects:[NSNumber       numberWithInt:label1.tag], [NSNumber numberWithInt:3],[NSNumber numberWithFloat:0.5f*time],[NSNumber numberWithFloat:timeDelay],[NSNumber numberWithFloat:0],nil];
                [animations addObject:animation1];
在虚空的尽头,我称之为[自我表演]

   - (void)performAnimations {
if ([[self animations] count] == 0) return;

for (int i=0; i<[[self animations] count]; i++) {

    int tag = [[[animations objectAtIndex:i]objectAtIndex:0]intValue];
    int animation = [[[animations objectAtIndex:i]objectAtIndex:1]intValue];
    float animTime = [[[animations objectAtIndex:i]objectAtIndex:2]floatValue];
    float animDelay = [[[animations objectAtIndex:i]objectAtIndex:3]floatValue];
    float parameter = [[[animations objectAtIndex:i]objectAtIndex:4]floatValue];

    UILabel *label = [UILabel alloc];
    UIView *view = [UIView alloc];
    if ([[self.view viewWithTag:tag] isKindOfClass:[UILabel class]]){
        label = (UILabel *)[self.view viewWithTag:tag];
    } else if ([[self.view viewWithTag:tag] isKindOfClass:[UIView class]]) {
        view = (UIView *)[self.view viewWithTag:tag];
    }

    switch (animation) {
        case 1:
            [PolyCalcsViewController colorizeLabelForAWhile:label withUIColor:[UIColor yellowColor] animated:YES withTime:animTime withDelay:animDelay];
            break;
        case 2:
            [PolyCalcsViewController appear:label withTime:animTime withDelay:animDelay];
            break;
        case 3:
            [PolyCalcsViewController disappear:label withTime:animTime withDelay:animDelay];
            break;
        case 4:
            [PolyCalcsViewController moveView:view withOffset:parameter withTime:animTime withDelay:animDelay];
            break;

        default:
            break;
    }

}

[animations removeAllObjects];
}


关键是程序并没有按照它在表格动画中的顺序执行动画。我想我应该放弃时间延迟,在上一个动画完成后运行下一个动画。我可以在completion:BOOL finished块中创建一个计数器,但我不知道如何创建,因为该函数不接受外部变量。你知道如何检测动画结束并控制表的执行吗?谢谢。

我终于解决了这个问题。这是一个正常工作的代码

 - (void)performAnimations {
// Finish when there are no more animations to run

int i = animationNumber;
int tag = [[[animations objectAtIndex:i]objectAtIndex:0]intValue];
int animation = [[[animations objectAtIndex:i]objectAtIndex:1]intValue];
float animTime = [[[animations objectAtIndex:i]objectAtIndex:2]floatValue];
//float animDelay = [[[animations objectAtIndex:i]objectAtIndex:3]floatValue];
float animDelay = 0;
float parameter = [[[animations objectAtIndex:i]objectAtIndex:4]floatValue];

UILabel *label = [UILabel alloc];
UIView *view = [UIView alloc];
if ([[self.view viewWithTag:tag] isKindOfClass:[UILabel class]]){
    label = (UILabel *)[self.view viewWithTag:tag];
} else if ([[self.view viewWithTag:tag] isKindOfClass:[UIView class]]) {
    view = (UIView *)[self.view viewWithTag:tag];
}

switch (animation) {
    case 1:
        [self colorizeLabelForAWhile:label withUIColor:[UIColor yellowColor] animated:YES withTime:animTime withDelay:animDelay];
        break;
    case 2:
        [self appear:label withTime:animTime withDelay:animDelay];
        break;
    case 3:
        [self disappear:label withTime:animTime withDelay:animDelay];
        break;
    case 4:
        [self moveView:view withOffset:parameter withTime:animTime withDelay:animDelay];
        break;

    default:
        break;
}
}

 -(void)animationDidFinished{
animationNumber = animationNumber + 1;
if (animationNumber == [[self animations] count]) {
    [animations removeAllObjects];
    return;
} else {
    [self performAnimations];
}
}


-(void)colorizeLabelForAWhile:(UILabel *)label withUIColor:(UIColor *)tempColor animated:(BOOL)animated withTime:(float)time withDelay:(float)delay{
UILabel *tempLabel = [[UILabel alloc] init];
tempLabel.textColor = tempColor;
tempLabel.font = label.font;
tempLabel.alpha = 0;
tempLabel.textAlignment = label.textAlignment;
tempLabel.text = label.text;
tempLabel.backgroundColor = [UIColor clearColor];
[label.superview addSubview:tempLabel];
tempLabel.frame = label.frame;

    if (animated) {
        [UIView animateWithDuration:time
                              delay:delay
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             // Animate it
                             label.alpha = 0;
                             tempLabel.alpha = 1;
                         } completion:^(BOOL finished){
                             [UIView animateWithDuration:time
                                                   delay:0
                                                 options:UIViewAnimationOptionCurveEaseInOut
                                              animations:^{
                                                  // Animate it back.
                                                  label.alpha = 1;
                                                  tempLabel.alpha = 0;
                                              } completion:^(BOOL finished){
                                                // Remove the tempLabel view when we are done.
                                                  [tempLabel removeFromSuperview];
                                                  if (finished) {
                                                      [self animationDidFinished];
                                                  }

                                              }];
                         }];

    } else {
        // Change it back at once and remove the tempLabel view.
        label.alpha = 1.0;
        [tempLabel removeFromSuperview];
    }
}


-(void)appear:(UILabel *)label withTime:(float)time withDelay:(float)delay{
    [UIView animateWithDuration:time
                          delay:delay
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         label.alpha = 1.0;
                     } completion:^(BOOL finished){
                         NSLog(@"Anim Appear %d",label.tag);
                         if (finished) {
                             [self animationDidFinished];
                         }
                     }];

 }

 -(void)disappear:(UILabel *)label withTime:(float)time withDelay:(float)delay{
[UIView animateWithDuration:time
                      delay:delay
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     label.alpha = 0.0;
                 } completion:^(BOOL finished){
                     NSLog(@"Anim Disappear %d",label.tag);
                     [label removeFromSuperview];
                     if (finished) {
                         [self animationDidFinished];
                     }

                 }];

 }

 -(void)moveView:(UIView *)view withOffset:(float)offset withTime:(float)time withDelay:(float)delay{
[UIView animateWithDuration:time
                      delay:delay
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     CGRect frame = view.frame;
                     frame.origin.y += offset;
                     view.frame = frame;
               } completion:^(BOOL finished){
                   if (finished) {
                       [self animationDidFinished];
                   }
               }
 ];
 }

其他所有动画都有什么问题?如何嵌套/链接/制作顺序动画等??同样的问题已经被问了很多次,只是有一些小的改动。尝试搜索这些。也许你会在右边的相关列表中看到一些东西…
   +(void)appear:(UILabel *)label withTime:(float)time withDelay:(float)delay{
    [UIView animateWithDuration:time
                          delay:delay
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         label.alpha = 1.0;
                     } completion:^(BOOL finished){
                         NSLog(@"Anim Appear %d",label.tag);
                     }];        
     }

    +(void)disappear:(UILabel *)label withTime:(float)time withDelay:(float)delay{
[UIView animateWithDuration:time
                      delay:delay
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     label.alpha = 0.0;
                 } completion:^(BOOL finished){
                     NSLog(@"Anim Disappear %d",label.tag);
                     [label removeFromSuperview];
                 }];    
        }
 - (void)performAnimations {
// Finish when there are no more animations to run

int i = animationNumber;
int tag = [[[animations objectAtIndex:i]objectAtIndex:0]intValue];
int animation = [[[animations objectAtIndex:i]objectAtIndex:1]intValue];
float animTime = [[[animations objectAtIndex:i]objectAtIndex:2]floatValue];
//float animDelay = [[[animations objectAtIndex:i]objectAtIndex:3]floatValue];
float animDelay = 0;
float parameter = [[[animations objectAtIndex:i]objectAtIndex:4]floatValue];

UILabel *label = [UILabel alloc];
UIView *view = [UIView alloc];
if ([[self.view viewWithTag:tag] isKindOfClass:[UILabel class]]){
    label = (UILabel *)[self.view viewWithTag:tag];
} else if ([[self.view viewWithTag:tag] isKindOfClass:[UIView class]]) {
    view = (UIView *)[self.view viewWithTag:tag];
}

switch (animation) {
    case 1:
        [self colorizeLabelForAWhile:label withUIColor:[UIColor yellowColor] animated:YES withTime:animTime withDelay:animDelay];
        break;
    case 2:
        [self appear:label withTime:animTime withDelay:animDelay];
        break;
    case 3:
        [self disappear:label withTime:animTime withDelay:animDelay];
        break;
    case 4:
        [self moveView:view withOffset:parameter withTime:animTime withDelay:animDelay];
        break;

    default:
        break;
}
}

 -(void)animationDidFinished{
animationNumber = animationNumber + 1;
if (animationNumber == [[self animations] count]) {
    [animations removeAllObjects];
    return;
} else {
    [self performAnimations];
}
}


-(void)colorizeLabelForAWhile:(UILabel *)label withUIColor:(UIColor *)tempColor animated:(BOOL)animated withTime:(float)time withDelay:(float)delay{
UILabel *tempLabel = [[UILabel alloc] init];
tempLabel.textColor = tempColor;
tempLabel.font = label.font;
tempLabel.alpha = 0;
tempLabel.textAlignment = label.textAlignment;
tempLabel.text = label.text;
tempLabel.backgroundColor = [UIColor clearColor];
[label.superview addSubview:tempLabel];
tempLabel.frame = label.frame;

    if (animated) {
        [UIView animateWithDuration:time
                              delay:delay
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             // Animate it
                             label.alpha = 0;
                             tempLabel.alpha = 1;
                         } completion:^(BOOL finished){
                             [UIView animateWithDuration:time
                                                   delay:0
                                                 options:UIViewAnimationOptionCurveEaseInOut
                                              animations:^{
                                                  // Animate it back.
                                                  label.alpha = 1;
                                                  tempLabel.alpha = 0;
                                              } completion:^(BOOL finished){
                                                // Remove the tempLabel view when we are done.
                                                  [tempLabel removeFromSuperview];
                                                  if (finished) {
                                                      [self animationDidFinished];
                                                  }

                                              }];
                         }];

    } else {
        // Change it back at once and remove the tempLabel view.
        label.alpha = 1.0;
        [tempLabel removeFromSuperview];
    }
}


-(void)appear:(UILabel *)label withTime:(float)time withDelay:(float)delay{
    [UIView animateWithDuration:time
                          delay:delay
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         label.alpha = 1.0;
                     } completion:^(BOOL finished){
                         NSLog(@"Anim Appear %d",label.tag);
                         if (finished) {
                             [self animationDidFinished];
                         }
                     }];

 }

 -(void)disappear:(UILabel *)label withTime:(float)time withDelay:(float)delay{
[UIView animateWithDuration:time
                      delay:delay
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     label.alpha = 0.0;
                 } completion:^(BOOL finished){
                     NSLog(@"Anim Disappear %d",label.tag);
                     [label removeFromSuperview];
                     if (finished) {
                         [self animationDidFinished];
                     }

                 }];

 }

 -(void)moveView:(UIView *)view withOffset:(float)offset withTime:(float)time withDelay:(float)delay{
[UIView animateWithDuration:time
                      delay:delay
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     CGRect frame = view.frame;
                     frame.origin.y += offset;
                     view.frame = frame;
               } completion:^(BOOL finished){
                   if (finished) {
                       [self animationDidFinished];
                   }
               }
 ];
 }