2011-05-04 76 views
9

我一直在使用這個偉大的文章http://oleb.net/blog/2010/12/animating-drawing-of-cgpath-with-cashapelayer中概述的CAShapeLayer繪製路徑,但我想知道是否有一種方法來動畫填充圖層。Animate繪製一個CAShapeLayer的填充

例如,我有一些我想在屏幕上繪製的文本,但我只能繪製文本的筆畫而不是填充。另一個例子,我有一個星形,我想動畫它被填充。

這是可能的使用CAShapeLayer或其他對象?

謝謝!

+0

你有什麼樣的動畫?移動中風?從底部到頂部或從左到右填充?褪色的顏色? – 2012-03-08 00:44:04

回答

-1

是的,這是可能的。

CAShapeLayers有一個可以生成動畫的fillColor屬性。

它與改變strokeEnd/strokeStart的方式一樣,就像你已經用動畫完成了一樣。

2

它的大部分時間是相同的代碼,你只需要爲你的CABasicAnimationfromValuetoValue設置不同的值。我創建了一個類別,它返回我CABasicAnimation

動畫StrokeEnd

+ (CABasicAnimation *)animStrokeEndWithDuration:(CGFloat)dur 
             delegate:(id)target{ 
    CABasicAnimation *animLine = 
    [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 
    [animLine setDuration:dur]; 
    [animLine setFromValue:[NSNumber numberWithFloat:0.0f]]; 
    [animLine setToValue:[NSNumber numberWithFloat:1.0f]]; 
    [animLine setRemovedOnCompletion:NO]; 
    [animLine setFillMode:kCAFillModeBoth]; 
    [animLine setDelegate:target]; 
    [animLine setTimingFunction: 
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 

    return animLine; 
} 

動畫用於填充顏色

+ (CABasicAnimation *)animFillColorWithDur:(CGFloat)dur 
            startCol:(UIColor *)start 
            endColor:(UIColor *)end 
            delegate:(id)target{ 

    CABasicAnimation *animFill = 
    [CABasicAnimation animationWithKeyPath:@"fillColor"]; 
    [animFill setDuration:dur]; 
    [animFill setFromValue:(id)start.CGColor]; 
    [animFill setToValue:(id)end.CGColor]; 
    [animFill setRemovedOnCompletion:NO]; 
    [animFill setDelegate:target]; 
    [animFill setFillMode:kCAFillModeBoth]; 

    return animFill; 
} 

返回CABasicAnimation剛剛已被添加到CAShapeLayer

[_myShapeLayer addAnimation:returnedAnimation forKey:@"animKey"]