One, Nstimer
| 123456789 |
//creation Method 1 nstimer *timer = [nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (action:) userinfo:nil repeats:no]; [timer invalidate]; //Call the Create method, the target object's counter will be incremented by 1 until the execution is complete, automatically minus 1. If it is a loop execution, it must be closed manually, otherwise the release method may not be performed. //recommended-Create Way 2 nstimer *timer = [nstimer timerwithtimeinterval:5 target:self selector: @selector (timerAction) Userinfo:nil repeats:yes]; [[nsrunloop mainrunloop] addtimer:timer formode:nsdefaultrunloopmode]; [timer invalidate]; |
Latency: The timing of the actual trigger event for a timer, whether it be a one-time or periodic, will be related to the Runloop and Runloop mode being added, and if the runloop is performing a continuous operation, the timer will be delayed.
Two, Cadisplaylink
| 123456789101112131415 |
- (void)startDisplayLink{ self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)]; [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];}- (void)handleDisplayLink:(CADisplayLink *)displayLink{ //do something}- (void)stopDisplayLink{ [self.displayLink invalidate]; self.displayLink = nil;} |
Cadisplaylink is a timer class that allows us to draw specific content to the screen at a frequency that is synchronized with the screen refresh rate. Once the Cadisplaylink is registered to Runloop in a specific mode, Runloop sends a specified selector message to the target specified by Cadisplaylink whenever the screen displays the end of the content refresh. The corresponding selector of the Cadisplaylink class is called once.
The screen refresh rate for iOS devices is fixed, and cadisplaylink is normally called at the end of each flush, with very high accuracy. The use of a relatively exclusive, suitable for the UI of the constant repainting, such as custom animation engine or video playback of the rendering. Do not need to be particularly concerned about the screen refresh frequency, itself is synchronized with the screen refresh.
Three, GCD
1. Perform a
| 12345678 |
doubledelayInSeconds = 2.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ //执行事件 }); |
2. Repeated execution
| 1234567 |
NSTIMEINTERVAL PERIOD = 1.0;  //set time interval dispatch_queue_t queue = dispatch_get_global_queue (dispatch_queue_priority_default, 0); Dispatch_source_t _timer = dispatch_source_create (Dispatch_source_type_timer, 0, 0, queue); dispatch_source_set_timer (_timer, dispatch_walltime (NULL, 0), period * nsec_per_sec, 0); //execute per second dispatch_source_set_event_handler (_timer, ^{ //execute event here;});      dispatch_resume (_timer); |
GCD timers and Nstimer are not the same, Nstimer affected by Runloop, but the GCD timer is not affected, because Runloop is also based on GCD
Timer (Global)
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172737475 |
@interface ViewController ()@property(nonatomic,strong)NSTimer *timer; // timer@property(nonatomic,assign)intcountDown; // 倒数计时用@property(nonatomic,strong)NSDate *beforeDate; // 上次进入后台时间 @endstaticintconsttick = 60;@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; [self setupNotification]; [self startCountDown];}-(void)dealloc { [[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil]; [[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil]; [self stopTimer];} -(void)setupNotification { [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(enterBG) name:UIApplicationDidEnterBackgroundNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(enterFG) name:UIApplicationWillEnterForegroundNotification object:nil];}/** * 进入后台记录当前时间 */-(void)enterBG { NSLog(@"应用进入后台啦"); _beforeDate = [NSDate date];}/** * 返回前台时更新倒计时值 */-(void)enterFG { NSLog(@"应用将要进入到前台"); NSDate * now = [NSDate date]; intinterval = (int)ceil([now timeIntervalSinceDate:_beforeDate]); intval = _countDown - interval; if(val > 1){ _countDown -= interval; }else{ _countDown = 1; }}/** * 开始倒计时 */-(void)startCountDown { _countDown = tick; //< 重置计时 _timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; //< 需要加入手动RunLoop,需要注意的是在NSTimer工作期间self是被强引用的 [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes]; //< 使用NSRunLoopCommonModes才能保证RunLoop切换模式时,NSTimer能正常工作。}-(void)timerFired:(NSTimer *)timer { if (_countDown == 0) { [self stopTimer]; NSLog(@"重新发送"); }else{ _countDown -=1; NSLog(@"倒计时中:%d",_countDown); }}- (void)stopTimer { if(_timer) { [_timer invalidate]; }} |
iOS three timer usage nstimer, Cadisplaylink, GCD