سؤال

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?

هل كانت مفيدة؟

المحلول 2

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.

نصائح أخرى

The only actual use of CAAction is as the basis of implicit animation - that is, it is at the heart of the mechanism by which an expression like

myLayer.position = // ... whatever ...

is magically able to animation the layer's change in position (assuming it is not the primary underlying layer of a UIView).

For a complete explanation of the action search mechanism, see my book:

http://www.apeth.com/iOSBook/ch17.html#_actions_2

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top