2014-12-01 3 views
0

Я бы хотел изменить скорость моего эффекта «Маршевые муравьи» после того, как он зациклировал каждые 10 раз. Таким образом, цикл анимации длится 1, после 10 циклов он должен изменить продолжительность до 0,95, через 10 секунд до 0,9 и так далее.Изменение скорости эффекта «маршевых муравьев» через каждые 10 секунд

У вас есть идея, как я мог это сделать ?

Вот код для моей текущей анимации:

CAShapeLayer *lineLayer = [CAShapeLayer layer]; 
    lineLayer.bounds = CGRectMake(0, 0, self.view.bounds.size.width, 20); 
    lineLayer.position = self.view.center; 

    lineLayer.strokeColor = [UIColor whiteColor].CGColor; 
    lineLayer.lineDashPattern = @[@100]; 
    lineLayer.lineWidth = CGRectGetHeight(lineLayer.bounds); 

    UIBezierPath *linePath = [UIBezierPath bezierPath]; 
    [linePath moveToPoint:CGPointMake(0, CGRectGetMidY(lineLayer.bounds))]; 
    [linePath addLineToPoint:CGPointMake(CGRectGetMaxX(lineLayer.bounds), CGRectGetMidY(lineLayer.bounds))]; 
    lineLayer.path = linePath.CGPath; 

    // [self.view.layer addSublayer:lineLayer]; 

    NSNumber *totalDashLenght = [lineLayer.lineDashPattern valueForKeyPath:@"@sum.self"]; // KVC is awesome :) 

    CABasicAnimation *animatePhase = [CABasicAnimation animationWithKeyPath:@"lineDashPhase"]; 
    animatePhase.byValue = totalDashLenght; // using byValue means that even if the layer has a shifted phase, it will shift on top of that. 
    animatePhase.duration = 0.5; 
    animatePhase.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
    animatePhase.repeatCount = 10; 

    // [lineLayer addAnimation:animatePhase forKey:@"marching ants"]; 


    // Create the shape layer 
    shapeLayer = [CAShapeLayer layer]; 
    CGRect shapeRect = CGRectMake(0.0f, 0.0f, 2200.0f, 2000.0f); 
    [shapeLayer setBounds:shapeRect]; 
    [shapeLayer setPosition:CGPointMake(160.0f, 1350.0f)]; 
    [shapeLayer setFillColor:[[UIColor clearColor] CGColor]]; 
    [shapeLayer setStrokeColor:[[UIColor whiteColor] CGColor]]; 
    [shapeLayer setLineWidth:20.0f]; 
    [shapeLayer setLineJoin:kCALineJoinRound]; 
    [shapeLayer setLineDashPattern: 
    [NSArray arrayWithObjects:[NSNumber numberWithInt:200], 
     [NSNumber numberWithInt:100], 
     nil]]; 


    // Setup the path 
    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathAddRect(path, NULL, shapeRect); 
    [shapeLayer setPath:path]; 
    CGPathRelease(path); 

    // Set the layer's contents 
    //[shapeLayer setContents:(id)[[UIImage imageNamed:@"GEIST.jpg"] CGImage]]; 

    [[[self view] layer] addSublayer:shapeLayer]; 


     if ([shapeLayer animationForKey:@"linePhase"]) 
      [shapeLayer removeAnimationForKey:@"linePhase"]; 
     else { 
      CABasicAnimation *dashAnimation; 
      dashAnimation = [CABasicAnimation 
          animationWithKeyPath:@"lineDashPhase"]; 

      [dashAnimation setFromValue:[NSNumber numberWithFloat:0.0f]]; 
      [dashAnimation setToValue:[NSNumber numberWithFloat:300.0f]]; 
      [dashAnimation setDuration:1]; 
      [dashAnimation setRepeatCount:10]; 
      [shapeLayer addAnimation:dashAnimation forKey:@"linePhase"]; 

ответ

0

Просто установите себе до detect the end of your animation, либо с помощью метода делегата или с блоком завершения по сделке. Когда ваша анимация завершит десять повторений, вы можете запланировать новую анимацию с меньшей продолжительностью.

(И помните, что он маленький, играя под столом и мечтая ...)

Смежные вопросы