2

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

Dimitris
  • 13,480
  • 17
  • 74
  • 94
  • 1
    Are we talking just any arbitrary float? Specific properties (like the `alpha` or `frame` properties, both of which rely on CGFloats at their core) can be animated with Core Animation blocks, but I don't think that extends to any float. – Tim Aug 26 '09 at 08:17

3 Answers3

2

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
  • 95,983
  • 54
  • 240
  • 345
  • 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 Aug 26 '09 at 16:10
2

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
  • 43,356
  • 8
  • 80
  • 90
1

I've been working on putting something of a framework together recently, part of which is a 'tweener'.

I'm still pretty new to obj c, but so far it seems to be serving my purposes reasonably well. Feel free to have a peek/play!

ShinobiTweener v0.1 for Objective-C

lznpde
  • 11
  • 1
  • Hi, I'm really interested in your tweening engine... I downloaded it but I can't find any .xcodeproj file in which to open all the files. Am I missing something? Thanks! – jowie Jul 07 '11 at 16:00
  • Sorry ignore my last comment... I hacked one together in the end :) looks like a great engine with lots of different transitions... Nicely done! :) – jowie Jul 07 '11 at 16:25