View Full Version : Call Method Automatically
Jul 21, 2011, 08:34 PM
I'm wondering if there is a way to call a method automatically in xcode. I need a method that is called continuously no matter what, but still leave room for other methods to run. I can't just use a while (1) statement to repeat this method over and over because my other methods (namely my GestureRecognizer methods) won't get called. So I'm wondering if there is a way to make a method that is called every cycle of code.
Jul 21, 2011, 09:12 PM
Please explain what needs to run continuously. Be specific.
There is no way to run something continuously. Periodically, yes. Continuously, no.
Periodic execution is what a repeating NSTimer is for. If you try to make it more continuous with a very short duration, you'll just bog down the performance of everything else.
You could also start a background NSThread, but it can't run continously either. If you try, it will also bog down performance, and you'll find that it doesn't run continuously anyway, because other threads will be scheduled which temporarily make the background thread inactive.
A repeating NSTimer will be much easier to get working than a background NSThread, so I recommend starting with the class reference doc for NSTimer.
Jul 21, 2011, 09:27 PM
I'm making a particle simulator, the user can create particles by touching the screen. I have a method that moves the particles according to each of their properties, etc. However, I need the particles to stay in motion even when the user isn't doing anything. I need the method that moves the particles continually called.
Jul 22, 2011, 02:42 PM
That's called animation. It's not continuous. It's discrete.
Animation proceeds frame by frame. Smooth animation has a constant frame rate. A varying frame rate will look jumpy.
A periodic NSTimer is capable of providing the animation timebase.
The max frame rate you can achieve will depend on the worst-case time consumed by the particle movement calculations, plus the particle rendering time.
Jul 22, 2011, 03:39 PM
xcodeNewbie, based on this and a number of other threads of yours recently, perhaps you should consider looking into using a 2D graphics engine (such as Cocos2D (http://www.cocos2d-iphone.org/)) that already supports such things as particle systems, collision detection, etc.