2012-08-25 70 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