动画后CAShapeLayer路径消失-需要它留在同一位置 [英] CAShapeLayer path disappears after animation - need it to stay in the same place

查看:120
本文介绍了动画后CAShapeLayer路径消失-需要它留在同一位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于对StackOverflow有所帮助,我目前正在为CAShapeLayer中的路径设置动画,以使三角形从移动的精灵指向屏幕上的另一个移动点。

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.

动画完成后,三角形将从屏幕上消失。我使用的持续时间很短,因为每个Sprite的代码每0.1秒被触发一次。结果是红色三角形正确跟踪,但快速闪烁或不完全闪烁。当我增加持续时间时,我可以看到三角形停留的时间更长。

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.

我可以做些什么来使三角形保持在屏幕上以及当前路径( tovalue ),直到再次调用该方法以从一个点到另一个点进行动画处理?我尝试设置removeedOnCompletion和removeAllAnimations,但无济于事。

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.

以下代码:

-(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,可在完成后删除动画。

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

执行以下操作:

connectorAnimation.fillMode = kCAFillModeForwards;

应该这样做。

这篇关于动画后CAShapeLayer路径消失-需要它留在同一位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆