Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
You would like to perform a specific task repeatedly with a certain delay. For instance, you want to update a view on your screen every second that your application is running.
Use a timer:
- (void) paint:(NSTimer *)paramTimer{
/* Do something here */
NSLog(@"Painting");
}
- (void) startPainting{
self.paintingTimer = [NSTimer
scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(paint:)
userInfo:nil
repeats:YES];
}
- (void) stopPainting{
if (self.paintingTimer != nil){
[self.paintingTimer invalidate];
}
}
- (void)applicationWillResignActive:(UIApplication *)application{
[self stopPainting];
}
- (void)applicationDidBecomeActive:(UIApplication *)application{
[self startPainting];
}