2012-08-05 101 views
4

是否可以在創建CGPath時(每次添加線時)繪製CGPath,而不是在瞬間繪製的點的整個路徑?我希望在添加線條時看到繪製的路徑。在構建路徑時繪製CGPath

另外,是否可以一次繪製一條線作爲較大路徑的一部分,而不是每次繪製一條路徑超過一條線?

回答

4

如果您使用CAShapeLayer並將路徑設置爲其路徑屬性,則可以爲strokeEnd值設置動畫。

我不知道這是多麼簡單的動態添加,但我以前使用此動畫製作具有定時點的線條(但預先知道這些線條的整個路徑)

3

@ wattson12是絕對正確的。下面是我如何在過去做了一個例子:

- (void)animateBezier 
{ 
    CAShapeLayer *bezier = nil; 
    UIBezierPath *bezierPath = [self bezierPath]; // clearly, generate your path anyway you want 

    bezier = [[CAShapeLayer alloc] init]; 

    bezier.path = bezierPath.CGPath; 
    bezier.strokeColor = [UIColor redColor].CGColor; 
    bezier.fillColor = [UIColor clearColor].CGColor; 
    bezier.lineWidth = 5.0; 
    bezier.strokeStart = 0.0; 
    bezier.strokeEnd = 1.0; 
    [self.view.layer addSublayer:bezier]; 

    CABasicAnimation *animateStrokeEnd = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 
    animateStrokeEnd.duration = 1.0; 
    animateStrokeEnd.fromValue = [NSNumber numberWithFloat:0.0f]; 
    animateStrokeEnd.toValue = [NSNumber numberWithFloat:1.0f]; 
    [bezier addAnimation:animateStrokeEnd forKey:@"strokeEndAnimation"]; 
} 

我不知道這是否是你想要什麼,或者你是否還要動畫後在路徑中添加額外的點。如果這是你想要的,那麼你可以做一些相應地調整fromValue的事情,或者將另一段的繪圖動畫化爲一個單獨的路徑。