1

我对动画有一些问题。这是我想要做的。首先,我有一个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 中,但我没有成功。可能我的知识还太少。欢迎任何帮助。谢谢!!!

4

2 回答 2

0

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

         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 结尾我调用 [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];
                 }];    
        }

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

于 2013-04-20T21:31:35.527 回答
0

最后我解决了这个问题。这是一个正确工作的代码

 - (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];
                   }
               }
 ];
 }
于 2013-04-22T13:48:11.790 回答