UIVIEW for循环中的链式动画



我有一些动画问题。这就是我要做的。首先,我有一个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;

标签有的出现,有的消失,有的变色。一切都很正常,直到我做第二个循环。然后代码的第一部分就出现了(改变颜色的动画仍然在工作,但是第一个循环中的所有标签都是可见的,并且没有动画。第二个循环动画很好。

我试图替换动画[UIView animateWithDuration:…]与CABasicAnimations和保存它们在一个NSMutableArray与队列,但我没有成功。可能我的知识还太短。欢迎任何帮助。谢谢! !

我正在解决问题。在循环中,我将动画参数保存在表格

         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 I call [self performAnimations];

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

}

  +(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];
                                              }];
                         }];
    } 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);
                     }];        
     }
    +(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];
                 }];    
        }

问题的关键在于,程序并没有像在表动画中那样按顺序执行动画。我认为我应该放弃时间延迟,并在上一个动画完成后运行下一个动画。我可以在完成(BOOL完成)块中做一个计数器,但我不知道如何,因为函数不接受外部变量。任何想法如何检测动画的结束和控制表的执行?谢谢。

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

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

最新更新