Timers You can create a timer that will call a chosen routine at regular intervals. It's very simple. Define a timer and an interval, NSTimeInterval drawingInterval; NSTimer *drawingTimer; Start it by setting the interval and calling this method, drawingInterval = 1.0 / 24.0;
drawingTimer = [NSTimer
scheduledTimerWithTimeInterval:drawingInterval
target:self
selector:@selector(drawView:)
userInfo:nil
repeats:YES];
This timer will call drawView 24 times each second. To stop the timer, [drawingTimer invalidate]; drawingTimer = nil;Notes: NSTimeInterval is not an object but a C variable, a double precision floating point number whose unit is seconds. If it is 0.0 or negative, the timer assumes a value of 0.1 milliseconds. The target method should have the signature, - (void)drawView:(NSTimer*)theTimer userInfo may be nil, or it may point to an object of your choice. The timer passes itself as an argument. It has a reference to userInfo which can be used for example, to determine which timer among several has called this method. If repeats is YES the timer fires regularly. If NO, it will fire once and invalidate itself automatically. The timer object is owned by the system and installed on the main loop. When you invalidate it, it is removed from the loop and released. It is a good idea to set the pointer to nil as a sign that the timer is no longer in existence. A timer created as above is automatically added to the main run loop. There are ways to create timers and add them to other loops. A timer can be set to start not at once but at some future time.
|