If you want to execute a specific task repeatedly, the task has a certain latency. 1. For example, if your program is running, you want to update the view on the screen every second:
-(Void) paint :( NSTimer *) paramTimer {NSLog (@ "Painting");}-(void) startPainting {self. paintingTimer = [NSTimerScheduledTimerWithTimeInterval: 1.0 target: self selector: @ selector (paint :) userInfo: nil repeats: YES];}-(void) stopPainting {If (self. paintingTimer! = Nil) {[self. paintingTimer invalidate]; // invalidate [? N'v & aelig; l? De? T] making invalid and worthless }}-(Void) applicationWillResignActive :( UIApplication *) application {[self stopPainting];}-(void) applicationDidBecomeActive :( UIApplication *) application {[self startPainting];}Once you call this function: scheduledTimerWithTimeInterval: target: selector: userInfo: repeats: the timer will become a scheduled timer, which will trigger events based on your request. A scheduled timer is added to an event processing loop. An example will be provided later: Create an unscheduled timer and manually schedule it in the main event processing cycle of the program. There are multiple methods to create, initialize, and schedule a timer. The simplest method is to use the NSTimer class method scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:. The following describes the parameters of this method:
Interval: wait for the number of seconds. The target object receiving the event selector. The method userInfo for responding to the event in the target object can pass information, including whether repeat is repeated in the passed timer.
You can use the NSTimer instance method invalidate to stop and release the timer. This not only releases the timer, but also releases the object owned by the timer (such as the userInfo object)
2.
You can also use other methods
Create a scheduled timer. The NSTimer class method scheduledTimerWithTimeInterval: invocation: repeats: is one of them:
-(Void) startPainting {// 2./* Here is the selector that we want to call */SEL selectorToCall = @ selector (paint :);// Convert to a method signature? Signature instanceNSMethodSignature * methodSignature = [[self class] instanceMethodSignatureForSelector: selectorToCall];//??NSInvocation * invocation =[NSInvocation invocationWithMethodSignature: methodSignature];[Invocation setTarget: self]; [invocation setSelector: selectorToCall];/* start a scheduled timer now */self. paintingTimer =[NSTimer scheduledTimerWithTimeInterval: 1.0Invocation: invocation repeats: YES];}3. if you want to manually schedule a timer at a specified time point in a program, you can use the NSTimer class method timerWithTimeInterval: target: selector: userInfo: repeats :, when you are ready, you can add the timer to the event processing cycle:
-(Void) startPainting {self. paintingTimer = [nstmerTimerWithTimeInterval: 1.0 target: self selector: @ selector (paint :) userInfo: nil repeats: YES];[[Nsunloop currentRunLoop] addTimer: self. paintingTimer forMode: NSDefaultRunLoopMode];}
GCD11: Create a timer