Путь CAShapeLayer исчезает после анимации — он должен оставаться на том же месте

StackOverflow https://stackoverflow.com/questions/2233601

Вопрос

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

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

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

Код ниже:

-(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;

}
Это было полезно?

Решение

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

Сделай это:

connectorAnimation.fillMode = kCAFillModeForwards;

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top