I recently wrote a custom view — a 3D vintage-looking pull lever — that provided a continuous property to control the state. I wanted to animated this smoothly, a-la CABasicAnimation, but couldn’t find a built-in way to do so.
So, I wrote a class that provides similar functionality to CABasicAnimation, but works on any object. I thought I’d share it.
Features:
- From/to value settings (currently only supports
NSNumber
and scalar numeric types, but easily extendable) - Duration, delay settings
- Timing functions: Linear, ease out, ease in, and ease in/ease out
- Animation chaining (specify another configured animation for
chainedAnimation
, and it’ll be fired once the first animation completes) - Delegate notification of animation completion
- Uses a single timer for smooth lock-step animation
- Uses
CADisplayLink
if available, to update in sync with screen updates
Use it like this:
- (void)startMyAnimation { TPPropertyAnimation *animation = [TPPropertyAnimation propertyAnimationWithKeyPath:@"state"]; animation.toValue = [NSNumber numberWithFloat:1.0]; // fromValue is taken from current value if not specified animation.duration = 1.0; animation.timing = TPPropertyAnimationTimingEaseIn; [animation beginWithTarget:self]; } |
Make sure you also include the QuartzCore framework, used to access CADisplayLink
, if it’s available.
It’s BSD-licensed.
Grab it here: TPPropertyAnimation.zip
Thank you for this code.