The CAShapeLayer path disappears after the animation - you need it to stay in the same place

Thanks to some help with StackOverflow, I am currently animating a path in CAShapeLayer to create a triangle that points from a moving sprite to another moving point on the screen.

After the animation is complete, the triangle disappears from the screen. I use very short durations because this code runs every .1 seconds for each of the sprites. As a result, the triangles of the red triangle are correct, but they blink quickly or not. When I check the duration, I see that the triangle stays longer.

What can I do to keep the triangle on the screen with the current tovalue until the method is called again to animate from place to place? I tried installing removeOnCompletion and removeAllAnimations, but to no avail.

Code below:

 -(void)animateConnector{ //this is the code that moves the connector from it 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; } 
+4
source share
1 answer

The problem is fillMode in your animation. The default for fillMode is "kCAFillModeRemoved", which will remove your animation upon completion.

Do it:

 connectorAnimation.fillMode = kCAFillModeForwards; 

That should do it.

+5
source

Source: https://habr.com/ru/post/1300820/


All Articles