I'm new to Core Animation. While learning about implicit animations, I came across a question.
I put a layer on the screen and made it color change to a random value when pressing a button, which triggers an implicit animation.
#define RANDOM_0_1 (arc4random() / (CGFloat)UINT_MAX)
- (IBAction)changeColor:(id)sender {
CGFloat r = RANDOM_0_1;
CGFloat g = RANDOM_0_1;
CGFloat b = RANDOM_0_1;
self.colorLayer.backgroundColor = [UIColor colorWithRed:r green:g blue:b alpha:1.0f].CGColor;
}
It works pretty well. Then I use
NSLog(@"%@", [self.colorLayer actionForKey:@"backgroundColor"]);
to get the animation object passed implicitly to the layer. I got
<CABasicAnimation: 0x7f8ae1d21970>.
Referring to the document, I learned that there are four way for a layer to get an action. 'Delegate, actions dictionary, style dictionary and +[CALayer defaultActionForKey:]' Then I stated wonder which step does the animation object really comes from. So I wrote this to check
NSLog(@"%@ %@ %@", self.colorLayer.delegate, self.colorLayer.actions, self.colorLayer.style);
It gave me three (null)
(null) (null) (null)
As the document said, these values should be set to nil by default.
So it must be +[CALayer defaultActionForKey:]
that gives me the animation object. However, when I call
NSLog(@"%@", [self.colorLayer actionForKey:@"backgroundColor"]);
it still gave me a (null).
That's quite strange I thought. I started wonder if the 'key' passed into has been somehow changed by the internal implementation. So I referred to this post to print the argument passed to the method as well as the return value.
static id<CAAction> (*__originalCALayerDefaultActionForKey)( CALayer *, SEL, NSString *) ;
static id<CAAction> CALayerDefaultActionForKey( CALayer * self, SEL _cmd, NSString * event )
{
id res = (*__originalCALayerDefaultActionForKey)( self, _cmd, event );
NSLog(@"%@<%p> %@ %@\n", [ self class ], self, event, res ) ;
return res;
}
I got the result like this
CALayer<0x106da5ef0> position (null)
CALayer<0x106da5ef0> bounds (null)
CALayer<0x106da5ef0> backgroundColor (null)
the 'key' passed into was exactly the property name, but it always returns null.
So, could anyone explain where does the animation object comes on earth, or provide me some technique to find out the answer?
Thanks. :)