0

iOs-coders!

This code to animate the little red square to drawing big sign "8" on the UIView. First an upper ring (animSequence1 = 5 sec), then right away a lower ring (animSequence2 = another 5 sec).
No delay needed!
But I stucked on strange delay (about 1 sec) between two sequential animations. Whence did this delay come from?!?

Code:

- (void)drawRect:(CGRect)drawRect {
   CGContextRef context = UIGraphicsGetCurrentContext();
   drawRect = CGRectMake(0, 0, 320, 320);
   CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor;);
   CGContextFillRect(context, drawRect);

   // Little red square
   CALayer *layerTest;
   layerTest = [CALayer layer];
   layerTest.bounds = CGRectMake(0, 0, 20, 20);
   layerTest.anchorPoint = CGPointMake(0, 0);
   layerTest.backgroundColor = [UIColor redColor].CGColor;
   [[self layer] addSublayer:layerTest];

   // Upper ring
   CAKeyframeAnimation *animSequence1;
   animSequence1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
   animSequence1.fillMode = kCAFillModeForwards;
   animSequence1.removedOnCompletion = NO;
   animSequence1.autoreverses = NO;
   animSequence1.repeatCount = 0;
   animSequence1.duration = 5.0;
   animSequence1.beginTime = 0.0;
   animSequence1.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 120) radius:80 startAngle:DEG_TO_RAD(90) endAngle:DEG_TO_RAD(450) clockwise:YES].CGPath;
   animSequence1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

   // Lower ring
   CAKeyframeAnimation *animSequence2;
   animSequence2 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
   animSequence2.fillMode = kCAFillModeForwards;
   animSequence2.removedOnCompletion = NO;
   animSequence2.autoreverses = NO;
   animSequence2.repeatCount = 0;
   animSequence2.duration = 5.0;
   animSequence2.beginTime = 5.0;
   animSequence2.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 280) radius:80 startAngle:DEG_TO_RAD(-90) endAngle:DEG_TO_RAD(-450) clockwise:NO].CGPath;
   animSequence2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

   // A sequence of animations
   CAAnimationGroup *animGroup;
   layerTest.position = CGPointMake(200, 200);
   animGroup = [CAAnimationGroup animation];
   animGroup.duration = 10.0;
   animGroup.animations = [NSArray arrayWithObjects:animSequence1, animSequence2, nil];

   [layerTest addAnimation:animGroup forKey:nil];
}

Also I tried make it without CAAnimationGroup.
Set up [animSequence1 setDelegate:self]; for first CAKeyframeAnimation (animSequence1), and then start second CAKeyframeAnimation (animSequence2) by (void)animationDidStop.
It worked same, but this strange delay (about 1 sec) between two animations no dissapear!

QUESTION: How to remove strange delay between two sequential CAKeyframeAnimation animations? No delay needed!

Nazar
  • 1
  • 2

1 Answers1

2

Don’t create layers or animations in -drawRect:—you don’t have a whole lot of control over when it gets called, so you’re going to end up with multiple layerTests added to your view, all trying to animate separately. Create the layer / animation in an initializer, or in response to some user interaction.

I would also advise getting rid of your fillMode and removedOnCompletion settings on animSequence1; once animSequence2 starts, you’re effectively asking CA to run two non-additive animations once on the same property. Not sure whether the behavior for that is well-defined, but it’s another place the weirdness could be coming from.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
  • Noah, thanks for advises. I moved my code to the `-(void)didMoveToWindow` method. And removed `fillMode` and `removedOnCompletion` from `animSequence1`. But the problem remains! There is a strange delay (approximately 1 second) between two consecutive animations `animSequence1` and `animSequence2`. Are there other idea how to remove this strange delay? – Nazar Oct 13 '17 at 20:40