2012-08-25 71 views
3

則strokeColor我一直不成功,在使用的答案從this previous線程動畫上CAShapeLayer閃爍的行程,很多的搜索後,我可以找到使用動畫的CABasicAnimation中風沒有其他例子。動畫效果CAShapeLayer與CABasicAnimation

我想要做的是有兩個顏色之間的脈衝我的CAShapeLayer脈衝。使用CABasicAnimation爲不透明工作正常,但[CABasicAnimation animationWithKeyPath:@"strokeColor"]逃避我,我會很感激任何有關如何成功實施的建議。

CABasicAnimation *strokeAnim = [CABasicAnimation animationWithKeyPath:@"strokeColor"]; 
    strokeAnim.fromValue   = (id) [UIColor blackColor].CGColor; 
    strokeAnim.toValue   = (id) [UIColor purpleColor].CGColor; 
    strokeAnim.duration   = 1.0; 
    strokeAnim.repeatCount  = 0.0; 
    strokeAnim.autoreverses  = NO; 
    [shapeLayer addAnimation:strokeAnim forKey:@"animateStrokeColor"]; 

// CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
// opacityAnimation.fromValue   = [NSNumber numberWithFloat:0.0]; 
// opacityAnimation.toValue   = [NSNumber numberWithFloat:1.0]; 
// opacityAnimation.duration   = 1.0; 
// opacityAnimation.repeatCount  = 0.0; 
// opacityAnimation.autoreverses  = NO; 
// [shapeLayer addAnimation:opacityAnimation forKey:@"animateOpacity"]; 

取消註釋不透明度動畫會導致預期的不透明度消失。筆畫動畫不起作用。一個隱式的strokeColor改變動畫的預期,但我希望記錄確認strokeColor可以使用CABasicAnimation顯式動畫。

更新:具體問題是shapeLayer.path爲NULL。糾正這個問題。

+0

躲閃你...怎麼樣?你有什麼嘗試?它怎麼不起作用?會發生什麼呢? –

回答

7

下面的代碼對我很好。你的shapeLayer筆畫路徑的lineWidth是什麼?這可能是問題嗎?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIBezierPath * circle = [UIBezierPath bezierPathWithOvalInRect:self.view.bounds]; 

    CAShapeLayer * shapeLayer = [CAShapeLayer layer]; 
    shapeLayer.path = circle.CGPath; 
    shapeLayer.strokeColor =[UIColor blueColor].CGColor; 
    [shapeLayer setLineWidth:15.0]; 

    [self.view.layer addSublayer:shapeLayer]; 

    CABasicAnimation *strokeAnim = [CABasicAnimation animationWithKeyPath:@"strokeColor"]; 
    strokeAnim.fromValue   = (id) [UIColor redColor].CGColor; 
    strokeAnim.toValue   = (id) [UIColor greenColor].CGColor; 
    strokeAnim.duration   = 3.0; 
    strokeAnim.repeatCount  = 0; 
    strokeAnim.autoreverses  = YES; 
    [shapeLayer addAnimation:strokeAnim forKey:@"animateStrokeColor"]; 

} 

讓我知道它是否適合你...

+0

謝謝,這證實了strokeColor可以用CABasicAnimation動畫。我的問題必須存在於其他地方(我懷疑是在之前的路徑渲染中還是在drawLayer:inContext中的後續渲染中)。當我發現確切的原因時,我會更新以供將來參考。 –

+0

更新線索與您的發現!我很想知道是什麼問題。 – clearwater82