Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Core animation to animate StrokeColor

I'm trying to animate CAShapeLayar's strokeColor property. I looked throught the documentation and it was stated that it is an animatable property. The code is working for animating postion.y but not the the strokeColor. I'll be happy to get any kind of help or advice

The code I've used:

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"];

Thanks in advance, Or.

like image 510
Or Ron Avatar asked Nov 22 '25 04:11

Or Ron


1 Answers

This should work, as I just tested it:

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.

Notice I removed the fromValue property line from the animation, since you want the same value as you set the strokeColor before the animation (its redundant). Also, I casted the strokeColor's CGColorRef to an id, which is exactly what the toValue property wants.

like image 139
MiguelB Avatar answered Nov 24 '25 18:11

MiguelB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!