Search code examples
iosmacoscore-animationquartz-core

What values can CAAction's runActionForKey:object:arguments: arguments take?


The CAAction protocol seems a bit poorly documented, and rarely commented on in books and blogs. The arguments for its sole method, runActionForKey:object:arguments:, are a bit mysterious. In my experience, the "object" argument always seems to be a CALayer, and the "arguments" dictionary is always nil.

It seems like the protocol might have been designed to be used in multiple places in Core Animation, otherwise why not define the type of the layer argument, and why have the extra dictionary that is never used? But as far as I can tell, it is only used with CALayer. Is that correct? Does anyone have any examples where the above conclusions are not true?


Solution

  • No, CAAction has not been used in any other APIs. When the protocol was designed it was left generic in case it became useful later, but nothing came up.

    It’s simple to define and invoke your own layer actions. A contrived example could be to dispatch a “mouseDown” action into the hit-tested layer passing some kind of event-specific data:

    CALayer *layer = myHitTest(…);
    [[layer actionForKey:@“mouseDown”]
     runActionForKey:@“mouseDown” object:layer arguments:@{…}];
    

    Here you’d not want the action to add an animation, so would implement the CAAction protocol on the class that handles the events, then put an instance of that class in the layer's actions dictionary.