Hammer
Hammer

Reputation: 8888

IOS animation, repeat sequence

I have a sequence of animation to show three different text of UILabel with transform.

    [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){
           }];
        }];
    }];

May I know how to repeat this sequence of animations? Put UIViewAnimationOptionRepeat on the outmost animation does not help.

Edited: thanks for the solution from Matt, the code changes to

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

May I know how to set the different text on the UIlabel for different animation? The code below does not work. The text change which should have happened when animation2 starts happens before animation1 starts, so animation1 shows the animation2's text.

Regards Hammer

Upvotes: 0

Views: 488

Answers (1)

matt
matt

Reputation: 535945

The problem is that you are trying to wrestle with view animation, which is a convenient front end for Core Animation — until it's not. It will be much easier if you would write a grouped animation directly at the Core Animation level. That way, you have a single complete "movie" that does your three animations in succession, and that can be easily repeated (it just repeats the whole "movie").

Upvotes: 2

Related Questions