IOS动画,重复序列

时间:2014-10-13 17:41:02

标签: ios core-animation

我有一系列动画来显示带有变换的UILabel的三个不同文本。

    [UIImageView animateWithDuration:2.0 delay:0.1 options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionRepeat animations:^{
        label.transform =CGAffineTransformMakeScale(1,1);
        label.text = [NSString stringWithFormat:@"Welcome %@!!!", name];
     }
     completion:^(BOOL finished){
       // code to run when animation completes
       // (in this case, another animation:)
      label.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.0, 0.0);
      [UIImageView animateWithDuration:3.0 delay:0.1 options:UIViewAnimationOptionCurveEaseOut animations:^{
          label.transform =CGAffineTransformMakeScale(1,1);
          label.text = [NSString stringWithFormat:@"Welcome %@!!!", name1];
        }
        completion:^(BOOL finished){
          [UIImageView animateWithDuration:1.0 delay:1.0 options:0
             animations:^{
                               label.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.0, 0.0);
               label.transform =CGAffineTransformMakeScale(1,1);
               label.text = [NSString stringWithFormat:@"Welcome %@", name3];
             }
           completion:^(BOOL finished){
           }];
        }];
    }];

我可以知道如何重复这一系列的动画吗?将UIViewAnimationOptionRepeat放在最外面的动画上没有帮助。

编辑:感谢Matt的解决方案,代码更改为

   CABasicAnimation *animation1 = [CABasicAnimation animation];
   label.text = [NSString stringWithFormat:@"Welcome %@!!!", name];
   animation1.keyPath = @"transform.scale";
   animation1.fromValue = [NSNumber numberWithFloat:0];
   animation1.toValue = [NSNumber numberWithFloat:1.0];
   animation1.duration = 2;


   CABasicAnimation *animation2 = [CABasicAnimation animation];
   UIFont *font=[UIFont fontWithName:@"Arial-BoldMT" size:120.0];
   int length = (int)[NSString stringWithFormat:@"%d", balance].length;
   NSString *pointString = [NSString stringWithFormat:@"welcome %@", name2];
   label.text = pointString;
   animation2.keyPath = @"transform.scale";
   animation2.fromValue = [NSNumber numberWithFloat:0];
   animation2.toValue = [NSNumber numberWithFloat:1.0];
   animation2.duration = 2;
   animation2.beginTime = animation1.duration - 0.1;

   CAAnimationGroup *group = [[CAAnimationGroup alloc] init];
   [group setAnimations:[NSArray arrayWithObjects:animation1, animation2,nil]];
   group.duration = animation1.duration + animation2.duration;
   [label.layer addAnimation:group forKey:@"basic"];

我是否知道如何在UIlabel上为不同的动画设置不同的文本?下面的代码不起作用。动画2启动时应该发生的文本更改发生在animation1启动之前,因此animation1显示了animation2的文本。

此致 锤

1 个答案:

答案 0 :(得分:2)

问题是你正试图与视频动画搏斗,这是核心动画的一个方便的前端 - 直到它不是。如果您直接在Core Animation级别编写分组动画,那么很多会更容易。这样,你就有了一个完整的“电影”,可以连续播放你的三部动画,可以轻松重复(它只是重复整个“电影”)。