I have a view with a sublayer called specialLayer
. Implicit animations for opacity are disabled like this:
self.specialLayer.actions = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNull null], @"opacity", nil];
There are only two methods which access this sublayer.
- (void)touchDown {
self.specialLayer.opacity = 0.25f;
}
- (void)touchUp {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.fromValue = [NSNumber numberWithFloat:self.specialLayer.opacity];
animation.toValue = [NSNumber numberWithFloat:1.0];
animation.duration = 0.5;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[self.specialLayer addAnimation:animation forKey:@"opacity"];
}
This happens:
1) User presses a button down (holding) and -touchDown gets called. specialLayer's opacity instantly becomes 0.25
2) User releases button and -touchUp gets called. specialLayer's opacity animates back to 1.0
This sequence only works ONCE!
3) User presses a button down again (holding) and -touchDown gets called. But specialLayer's opacity does not change on screen! Nothing happens even though self.specialLayer.opacity = 0.25f;
gets called. Opacity appears to remain 1.0f.
4) User lifts finger up and -touchUp gets called. specialLayer's opacity JUMPS to 0.25 and from there animates to 1.0f.
I checked with NSLog to make sure the methods get called in this order. They do.
When I uncomment the animation code and set the opacity directly to 1.0, subsequent direct opacity changes are effective immediately. It is definitely the CABasicAnimation which causes problems. Removing the code that disables implicit animations has no effect on this problem.
I can't for the life of me figure out what is happening here. Why does CALayer not reflect the opacity value I set to it some time later after I added a CABasicAnimation for opacity? Is this a bug?
What am I doing wrong?