【问题标题】:CAShapeLayer path disappears after animation - need it to stay in the same placeCAShapeLayer 路径在动画后消失 - 需要它留在同一个地方
【发布时间】:2010-02-10 00:38:39
【问题描述】:

感谢 StackOverflow 上的一些帮助,我目前正在为 CAShapeLayer 中的路径设置动画,以制作一个三角形,该三角形从移动的精灵指向屏幕上的另一个移动点。

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

我可以做些什么来让三角形停留在屏幕上,它的当前路径(tovalue)直到再次调用该方法以从该点动画到下一个点?我尝试设置removedOnCompletion 和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;

}

【问题讨论】:

  • 我的回答对您有帮助吗?如果有,您能否将其标记为已回答?

标签: iphone core-animation core-graphics cashapelayer


【解决方案1】:

问题是动画中的 fillMode。 fillMode 的默认值是“kCAFillModeRemoved”,它会在完成时移除您的动画。

这样做:

connectorAnimation.fillMode = kCAFillModeForwards;

应该这样做。

【讨论】:

  • 谢谢你,也帮助了我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多