First:
Cadisplaylink can be used for non-stop re-painting.
Example:
Cadisplaylink * gametimer;
Gametimer = [cadisplaylink displaylinkwithtarget: Self
Selector: @ selector (updatedisplay :)];
[Gametimer addtorunloop: [nsunloop currentrunloop]
Formode: nsdefaultrunloopmode];
Second:
Int ccapplication: Run () {If (applicationdidfinishlaunching () {[[ccdirectorcaller shareddirectorcaller] startmainloop]; // The start of the Main Loop} return 0 ;}
Continue with startmainloop Function
-(Void) startmainloop {// ccdirector: setanimationinterval () is called, we shocould invalidate it first [displaylink invalidate]; displaylink = nil; // displaylink is the cadisplaylink object, target is your own, And the callback is cocaller displaylink = [nsclassfromstring (@ "cadisplaylink") displaylinkwithtarget: Self selector: @ selector (docaller :)]; // check the docaller callback [displaylink setframeinterval: self. interval]; // set the frame rate [displaylink addtorunloop: [nsunloop currentrunloop] formode: nsdefaultrunloopmode]; // Add to loop and start}
View docaller callback,
void CCDisplayLinkDirector::mainLoop(void){ if (m_bPurgeDirecotorInNextLoop) { m_bPurgeDirecotorInNextLoop = false; purgeDirector(); } else if (! m_bInvalid) { drawScene();// draw the scene // release the objects CCPoolManager::sharedPoolManager()->pop(); }}
Okay. A loop is finished. The ccpoolmanager: sharedpoolmanager ()-> POP (); is used to release the object.
Third: IOS -- nstimer and cadisplaylink usage nstimer initializer accepts the interval between calling method logic as one of its parameters. It is scheduled to run 30 times a second. Cadisplaylink runs 60 times per second by default. Its frameinterval attribute changes the number of frames running per second. If it is set to 2, cadisplaylink runs once every frame and the valid logic runs 30 times per second.
In addition, nstimer accepts whether another parameter is repeated, and sets cadisplaylink to be repeated (the default parameter is repeated ?) Until it becomes invalid.
Another difference is that nstimer starts to run once it is initialized, and cadisplaylink needs to add the display link to a running loop, that is, a cocoa touch structure used to process system events.
Nstimer is usually used in background computing to update some numerical data. If it involves image Update and the evolution of the animation process, we usually use cadisplaylink.
However, to use cadisplaylink, you must add quartzcore. Framework and # import <quartzcore/cadisplaylink. h>
Nstimer
@ Interface viewcontroller: uiviewcontroller
{
Nstmer * thetimer; // Declaration
}
// Use
Float theinterval = 1.0/30366f; // 30 calls per second
Thetimer = [nstimer scheduledtimerwithtimeinterval: theinterval target: Self selector: @ selector (mytask) userinfo: Nil repeats: Yes];
// Disable
[Thetimer invalidate];
Thetimer = nil;
Cadisplaylink. quartzcore. Framework and # import <quartzcore/cadisplaylink. h> must be added.
/* Cadisplaylink runs 60 times per second by default, and sets its frameinterval attribute to 2, meaning that cadisplaylink runs once every frame, effectively making the game logic run 30 times per second */
If (thetimer = nil)
{
Thetimer = [cadisplaylink displaylinkwithtarget: Self selector: @ selector (mytask)];
Thetimer. frameinterval = 2;
[Thetimer addtorunloop: [nsunloop currentrunloop] formode: nsdefaultrunloopmode];
}
// Disable
[Thetimer invalidate];
Thetimer = nil;
Cadisplaylink and timer usage