2017-02-02 1 views
0

Ich habe einen UIBezier-Pfad animiert, um einen Kreis zu durchlaufen und dabei seine Strichfarbe zu ändern. Wenn es um den ganzen Weg geht, möchte ich, dass es in die entgegengesetzte Richtung geht.Wie man einen Pfad vorwärts und rückwärts animiert?

Hier ist der Code, der es nach vorne gehen macht:

zur ersten Animation
[CATransaction begin]; 
    { 
     [CATransaction setAnimationDuration: 3.0];//Dynamic Duration 
     [CATransaction setCompletionBlock:^{ 
      [tutorialCircle removeFromSuperlayer]; 
      tutorialCircle.opacity = 0.0; 
      strokePart.opacity = 0.0; 
      [strokePart removeFromSuperlayer]; 
     }]; 

     const double portion = 1.0/((double)3); 

     for (NSUInteger i = 0; i < 3; i++) 
     { 
      strokePart = [[CAShapeLayer alloc] init]; 
      strokePart.fillColor = [[UIColor clearColor] CGColor]; 
      strokePart.frame = tutorialCircle.bounds; 
      strokePart.path = tutorialCircle.path; 
      strokePart.lineCap = tutorialCircle.lineCap; 
      strokePart.lineWidth = tutorialCircle.lineWidth; 

      if (i == 0) { 
       strokePart.strokeColor = [UIColor greenColor].CGColor; 
      } 

      else if (i == 1) { 
       strokePart.strokeColor = [UIColor orangeColor].CGColor; 
      } 

      else if (i == 2) { 
       strokePart.strokeColor = [UIColor redColor].CGColor; 
      } 
      strokePart.strokeStart = i * portion; 
      strokePart.strokeEnd = (i + 1) * portion; 

      [tutorialCircle addSublayer: strokePart]; 

      animation = [CAKeyframeAnimation animationWithKeyPath: @"strokeEnd"]; 
      NSArray* times = @[ @(0.0), // Note: This works because both the times and the stroke start/end are on scales of 0..1 
           @(strokePart.strokeStart), 
           @(strokePart.strokeEnd), 
           @(1.0) ]; 
      NSArray* values = @[ @(strokePart.strokeStart), 
           @(strokePart.strokeStart), 
           @(strokePart.strokeEnd), 
           @(strokePart.strokeEnd) ]; 

      animation.keyTimes = times; 
      animation.values = values; 
      animation.removedOnCompletion = YES; 
      animation.fillMode = kCAFillModeForwards; 
      [animation setDelegate:self]; 
      [strokePart addAnimation: animation forKey: @"whatever"]; 
     } 
    } 
    [CATransaction commit]; 

Also in meinem animationDidStop Delegatmethode, ich weiß, ich würde die zweite Animation anrufen rückwärts zu gehen, aber ich bin kämpfen, um die Animation dafür zu schaffen, rückwärts zu gehen.

Antwort

0

Haben Sie es versucht?

animation.autoreverses = YES

+0

Ja es fast genau das tut, was ich will, aber es blitzt verschiedene Farben, wenn sie zurückkommt –

Verwandte Themen