使用核心animation来为StrokeColor设置animation

我试图animationCAShapeLayar的strokeColor属性。 我查看了整个文档,并指出这是一个可以animation的属性。 该代码正在为postion.yanimation,但不是strokeColor。 我会很高兴得到任何forms的帮助或build议

我用过的代码:

lyr.fillColor = [[UIColor clearColor] CGColor]; lyr.lineWidth = 3.8; lyr.strokeColor = [[UIColor yellowColor] CGColor]; lyr.position = CGPointMake(160, 431); lyr.anchorPoint = CGPointMake(.5f, .5f); [self.view.layer addSublayer:lyr]; CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"strokeColor"]; basicAnimation.fromValue = [lyr valueForKey:@"strokeColor"]; basicAnimation.toValue =[NSValue valueWithPointer:[[UIColor clearColor] CGColor]]; basicAnimation.duration = 2; basicAnimation.repeatCount = 10; basicAnimation.autoreverses = YES; [lyr addAnimation:basicAnimation forKey:@"strokeColor"]; 

在此先感谢,或。

这应该工作,因为我只是testing它:

 CABasicAnimation *strokeAnim = [CABasicAnimation animationWithKeyPath:@"strokeColor"]; // I changed the animation pointer name, but don't worry. basicAnimation.toValue = (id) [UIColor clearColor].CGColor; // Just get rid of the fromValue line, and just cast the CGColorRef to an id. basicAnimation.duration = 2; basicAnimation.repeatCount = 10; basicAnimation.autoreverses = YES; [lyr addAnimation:strokeAnim forKey:@"flashStrokeColor"]; // I like to name this key differently, so as not create confusion. 

注意我从animation中删除了fromValue属性行,因为你需要在animation(它的冗余)之前设置strokeColor的值。 另外,我将strokeColor的CGColorRef转换为一个id,这正是toValue属性所需要的。

接受的答案代码的一个重要陷阱是,addAnimation方法的forKey:参数需要是隐式animation的属性的名称,您希望防止其发生。 由于您正在使用CABasicAnimation创build显式animation,因此您不希望隐式animation和新创build的显式animation相互干扰。 有关确认,请参阅WWDC 2010video会话424核心animation – 实践中的核心animation,第1部分,以39分钟为标记。