Combine a CALayer Position Animation with a value animation … ???
I came around this problem during a project where we should move a formatted text containing a number like, e.g. „It took x seconds to complete the task“. During the movement x should be incremented to following the timing function of the position animation.
I have to confess I had to think about it twice but shortly I thought CALayer can animation almost anything, so why not a custom variable.
So here is the basic idea.
I created a basic CALayer with our count instance variable which gives us the current count value during the position animation.
The implementation is as simple a telling our CounterLayer that we need an information whenever our count value changes. This is done with
+ (BOOL)needsDisplayForKey:(NSString *)key
method. This forces the layer to call its delegate
– (void)displayLayer:(CALayer *)layer
quite early in its update cycle to give the delegate the chance to update the contents property directly.
As we are only interested in the count’s current value, we grab it from the presentation layer for further use.
You can find the whole project on GitHub.