1

I have this piece of code that works perfectly in one of my other projects where I am achieving a 'strobe' effect of text flashing from black to white on a loop. When I copied and pasted it into another one of my projects, the CompletionBlock fires immediately, ignoring the animation duration. What could be the reason?

- (void)animateTextFlashingWhite
{
    [CATransaction begin];
    [CATransaction setCompletionBlock:^{
        [self animateTextFlashingBlack];
        NSLog(@"finished white");
    }];
    [CATransaction setValue:[NSNumber numberWithFloat:0.7f] forKey:kCATransactionAnimationDuration];
    self.myStrobeLabel.textColor = [UIColor whiteColor];
    [CATransaction commit];
}

- (void)animateTextFlashingBlack
{
    [CATransaction begin];
    [CATransaction setCompletionBlock:^{
        [self animateTextFlashingWhite];
        NSLog(@"finished black");
    }];
    [CATransaction setValue:[NSNumber numberWithFloat:0.7f] forKey:kCATransactionAnimationDuration];
    self.myStrobeLabel.textColor = [UIColor blackColor];
    [CATransaction commit];
}
Kevin_TA
  • 4,575
  • 13
  • 48
  • 77

1 Answers1

2

I don't think textColor is animatable.

If you simply want a crossfade, you can accomplish this by adding a CATransition object to the label.

[self.myStrobeLabel.layer addAnimation:[CATransition animation] forkey:@"transition"];
self.myStrobeLabel.textColor = [UIColor blackColor];
Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • At a glance, this looks like it will just change the text to black. How would I get it to go from black to white and vice versa on a loop? Is there a completion block setting? Also, where is the duration defined? Thanks. – Kevin_TA Mar 18 '13 at 22:53
  • @Kevin_TA: `CATransition` conforms to `CAMediaTiming` so you can set duration and whatnot there. As for using a loop, maybe just set up an `NSTimer`? – Lily Ballard Mar 18 '13 at 22:54
  • @Kevin_TA: It's also possible that the `CATransition` itself will be usable with a `CATransaction`, and thus enabling the use of completion blocks there, but I've never tried that myself so I don't know if that will work. – Lily Ballard Mar 18 '13 at 22:55