Путь CAShapeLayer исчезает после анимации - нужно, чтобы он оставался на том же месте - PullRequest
4 голосов
/ 10 февраля 2010

Благодаря некоторой помощи в StackOverflow я в настоящее время анимирую путь в CAShapeLayer, чтобы создать треугольник, который указывает от движущегося спрайта на другую движущуюся точку на экране.

После завершения анимации треугольник исчезает с экрана. Я использую очень короткие промежутки времени, потому что этот код запускается каждые 0,1 секунды для каждого из спрайтов. В результате красный треугольник отслеживает правильно, но он быстро мигает или не совсем. Когда я проверяю продолжительность, я вижу, как треугольник остается дольше.

Что я могу сделать, чтобы треугольник остался на экране, с его текущим путем (tovalue), пока метод не будет вызван снова для анимации от места к следующему месту? Я попытался установить удаленное завершение и удаление всех анимаций, но безрезультатно.

Код ниже:

-(void)animateConnector{

//this is the code that moves the connector from it's current point to the next point
//using animation vs. position or redrawing it

    //set the newTrianglePath
    newTrianglePath = CGPathCreateMutable();
    CGPathMoveToPoint(newTrianglePath, nil, pointGrid.x, pointGrid.y);//start at bottom point on grid
    CGPathAddLineToPoint(newTrianglePath, nil, pointBlob.x, (pointBlob.y - 10.0));//define left vertice
    CGPathAddLineToPoint(newTrianglePath, nil, pointBlob.x, (pointBlob.y + 10.0));//define the right vertice
    CGPathAddLineToPoint(newTrianglePath, nil, pointGrid.x, pointGrid.y);//close the path
    CGPathCloseSubpath(newTrianglePath);
    //NSLog(@"PointBlob.y = %f", pointBlob.y);

    CABasicAnimation *connectorAnimation = [CABasicAnimation animationWithKeyPath:@"path"];`enter code here`
    connectorAnimation.duration = .007; //duration need to be less than the time it takes to fire handle timer again
    connectorAnimation.removedOnCompletion = NO;  //trying to keep the the triangle from disappearing after the animation
    connectorAnimation.fromValue = (id)trianglePath;  
    connectorAnimation.toValue = (id)newTrianglePath;
    [shapeLayer addAnimation:connectorAnimation forKey:@"animatePath"];


    //now make the newTrianglePath the old one, so the next animation starts with the new position 2.9-KC
    self.trianglePath = self.newTrianglePath;

}

1 Ответ

5 голосов
/ 21 февраля 2010

Проблема в том, что в вашей анимации используется fillMode. Значением по умолчанию для fillMode является «kCAFillModeRemoved», который удалит вашу анимацию после завершения.

Сделайте это:

connectorAnimation.fillMode = kCAFillModeForwards;

Это должно сделать это.

...