Animating the drawing of a line

Posted by jkigel on Stack Overflow See other posts from Stack Overflow or by jkigel
Published on 2012-05-15T14:53:54Z Indexed on 2012/06/17 21:17 UTC
Read the original article Hit count: 220

Filed under:
|
|
|
|

I'm trying to animate the drawing of a line by the following way:

.h


    CAShapeLayer *rootLayer;
    CAShapeLayer *lineLayer;
    CGMutablePathRef path;

.m


        path = CGPathCreateMutable();
        CGPathMoveToPoint(path, nil, self.frame.size.width/2-100, 260);
        CGPathAddLineToPoint(path, nil, self.frame.size.width/2+100.0, 260);    
        CGPathCloseSubpath(path);

        self.rootLayer = [CALayer layer];
        rootLayer.frame = self.bounds;
        [self.layer addSublayer:rootLayer];

        self.lineLayer = [CAShapeLayer layer];
        [lineLayer setPath:path];
        [lineLayer setFillColor:[UIColor redColor].CGColor];
        [lineLayer setStrokeColor:[UIColor blueColor].CGColor];
        [lineLayer setLineWidth:1.5];
        [lineLayer setFillRule:kCAFillRuleNonZero];
        [rootLayer addSublayer:lineLayer];

        [self performSelector:@selector(startTotalLine) withObject:nil afterDelay:1.5];

- (void)startTotalLine
{    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"animatePath"];
    [animation setDuration:3.5];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [animation setAutoreverses:NO];
    [animation setFromValue:(id)path];
    [animation setToValue:(id)path];
    [lineLayer addAnimation:animation forKey:@"animatePath"];    
}

The line had drawn before the startTotalLine method is invoked. Also, the startTotalLine method doesn't affect the line.

I want it to animate the the line drawing from right to left

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c