GCD11: Create a timer

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.