iPhone CALСлойная анимация

Я рисую линейную анимацию, соединяя 2 точки линией. Это мой код:

-(void) drawObject{     
    rootLayer   = [CALayer layer];
    rootLayer.frame = self.view.bounds;
    [self.view.layer addSublayer:rootLayer];

    lineStartPath = CGPathCreateMutable();
    CGPathMoveToPoint(lineStartPath, nil, 160, 50);
    CGPathAddLineToPoint(lineStartPath, nil, 160, 50);
    CGPathCloseSubpath(lineStartPath);

    lineEndPath = CGPathCreateMutable();
    CGPathMoveToPoint(lineEndPath, nil, 160, 50);
    CGPathAddLineToPoint(lineEndPath, nil, 160, 300);
    CGPathCloseSubpath(lineEndPath);

    shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = lineStartPath;
    UIColor *strokeColor = [UIColor colorWithHue:0.557 saturation:0.55 brightness:0.96 alpha:1.0];
    shapeLayer.strokeColor = strokeColor.CGColor;
    shapeLayer.lineWidth = 3.0;

    [rootLayer addSublayer:shapeLayer];

    [self performSelector:@selector(startAnimation) withObject:nil afterDelay:1.0];
}

-(void) startAnimation{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
    animation.duration = 0.5;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.fromValue = (id)lineStartPath;
    animation.toValue = (id)lineEndPath;
    [shapeLayer addAnimation:animation forKey:@"animatePath"];
}

Слабое место - после окончания анимации линия пропадает. Я хочу, чтобы после того, как моя линия проведена между этими двумя точками, id не исчезал. Подскажите пожалуйста.


person Oleg Danu    schedule 21.02.2011    source источник


Ответы (1)


Попробуйте добавить

shapeLayer.path = lineEndPath;

в начале startAnimation.

Это объясняется в этом видео: Сессия 424 - Базовая анимация на практике, часть 1 с WWDC. 2010 г. (около 38:00).

и замените [shapeLayer addAnimation:animation forKey:@"animatePath"]; на [shapeLayer addAnimation:animation forKey:@"path"];.

person Jilouc    schedule 21.02.2011
comment
Ключ при добавлении анимации может быть любым, который вы хотите, он используется только для возврата анимации с помощью [myLayer animationForKey:@"myKey"]; - person David Rönnqvist; 13.07.2012