Question

I've got an NSButton called _indicatorButton with a PNG image (basically a green LED light as PNG set with setImage:) without border and text. I'm trying to make it "flash" with the following code:

- (void)animateButton
{
    NSLog(@"Animating…");
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    [animation setFromValue:[NSNumber numberWithFloat:1.0]];
    [animation setToValue:[NSNumber numberWithFloat:0.0]];
    [animation setDuration:0.3f];
    [animation setTimingFunction:[CAMediaTimingFunction
                                  functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [animation setAutoreverses:YES];
    [animation setRepeatCount:20000];

    [_indicatorButton.layer addAnimation:animation forKey:@"opacity"];
}

…but nothing happens. The method gets called and the link in the XIB is OK, since changing the image on the button programmatically works fine.

Any hints why it doesn't blink at all?

Was it helpful?

Solution

For backwards compatibility, views on OS X don't use Core Animation layers as their backing store by default. The reason nothing is happening is likely that layer of your button is nil.

You can tell your view that it should be layer-backed using myView.wantsLayer = YES;. Please read the documentation for wantsLayer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top