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

戏子无情 提交于 2019-12-22 08:11:26

问题


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

Once the animation completes, the triangle disappears from the screen. I am using very short durations because this code is being fired every .1 of second for each of the sprites. The result is the red triangle tracks correctly, but it rapidly flashes or isn't there entirely. When I crank up the the duration, I can the see the triangle stay longer.

What can I do to get the triangle to stay on the screen, with it's current path (the tovalue) until the method is called again to animate from the spot to the next spot? I tried setting the removedOnCompletion and removeAllAnimations, but to no avail.

Code below:

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


The issue is the fillMode on your animation. The default for fillMode is "kCAFillModeRemoved" which will remove your animation when completed.

Do this:

connectorAnimation.fillMode = kCAFillModeForwards;

This should do it.



来源:https://stackoverflow.com/questions/2233601/cashapelayer-path-disappears-after-animation-need-it-to-stay-in-the-same-place

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!