每10秒后改变“行进蚂蚁”效果的速度

时间:2014-12-01 15:09:03

标签: xcode performance duration marching-ants

我想改变我的行动速度"行动蚂蚁"它每10次循环后效果。所以它以1的持续时间循环动画,在10个循环之后它应该将持续时间改为0.95,再过10秒后再改为0.9等等......

你知道我该怎么做吗?

这是我当前动画的代码:

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

1 个答案:

答案 0 :(得分:0)

使用委托方法或在事务上使用完成块,只需将自己设置为detect the end of your animation即可。当动画完成十次重复时,您可以安排持续时间较短的新动画。

(记住小,在桌子下面玩,做梦......)