views:

541

answers:

3

Is it possible to tween a variable (let's say a float from 0.0 to 2.0 over a period of time) in objective-C? Something like what TweenMax would do in flash. I guess the class methods of UIView don't do that. Is it doable maybe with CoreAnimation? Or would someone have to use NSTimer?

Thanks

+2  A: 

Use an NSTimer on a selector that increments a member variable from its start value of 0.0 by the increment value.

When the variable reaches the end point (2.0), invalidate the NSTimer instance to finish incrementing.

See the documentation for more information about the method to use.

Alex Reynolds
This is relatively easy, but still there is an overhead in taking care of the NSTimer. And it's hard to play easing etc. Is there another way to do it, in less code?
Dimitris
Not that I know of.
Alex Reynolds
+1  A: 

What are you doing? If you are trying to animate something then you can use CA and it will deal with calculating the intermediary values over time.

If you are trying to do for something not related to views or animation you will need to do it yourself (using a timer, or a custom property implementation that dynamically calculates the value based on the current times when it is accessed).

Louis Gerbarg
A: 

This is a real shame, NSTimer won't give you a tween... I'm generating my view by drawing the positions of objects in a z plane, so I want to be able to tween the z value (with an easeInOut), and read the animation each time it updates...

Is there no way for CA to send onUpdate events?

Joe