Путь CAShapeLayer исчезает после анимации - нужно, чтобы он оставался на том же месте

Благодаря некоторой помощи в 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;

}

person KLC    schedule 10.02.2010    source источник
comment
Помог ли мой ответ вам в этом вопросе, и если да, можете ли вы отметить ответ?   -  person bstahlhood    schedule 30.08.2010


Ответы (1)


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

Сделай это:

connectorAnimation.fillMode = kCAFillModeForwards;

Это должно сработать.

person bstahlhood    schedule 21.02.2010
comment
Спасибо за это, мне тоже помогли - person Kuzushi Nakamoto; 28.12.2013