iOS Call Method Automatically

xcodeNewbie

macrumors member
Original poster
Jul 1, 2011
65
0
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.
 

chown33

Moderator
Staff member
Aug 9, 2009
8,671
4,942
vertical
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.
 
Comment

xcodeNewbie

macrumors member
Original poster
Jul 1, 2011
65
0
Periodic Function

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.
 
Comment

chown33

Moderator
Staff member
Aug 9, 2009
8,671
4,942
vertical
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.
 
Comment

dejo

Moderator
Staff member
Sep 2, 2004
15,981
450
The Centennial State
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) that already supports such things as particle systems, collision detection, etc.
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.