2010-02-10 69 views
4

感謝在StackOverflow上的一些幫助,我目前正在動畫CAShapeLayer中的一個路徑,以創建一個從移動的精靈指向屏幕上另一個移動點的三角形。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; 

} 
+0

我的回答對你有幫助嗎?如果是這樣,你能標記它嗎? – bstahlhood 2010-08-30 18:15:24

回答

5

的問題是,在你的動畫在fillMode。 fillMode的默認值是「kCAFillModeRemoved」,它將在完成後刪除動畫。

這樣做:

connectorAnimation.fillMode = kCAFillModeForwards; 

這應該這樣做。

+0

感謝你的幫助,我也出來了 – 2013-12-28 09:07:55