In the daily development, we often have to delay waiting (such as network requests, UI updates) and then do something, or do some periodic processing. This time requires us to implement a periodic timer to delay the operation. The following three methods are commonly used for time-lapse implementations:
1.NStimer implementations
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 this runloop is performing a continuous operation, the timer will be triggered by the delay. A repetitive timer encounters this situation, and if the delay exceeds a period, it is executed immediately after the delay and continues with the period specified earlier. (PS: So sometimes using a timer, there will occasionally be two consecutive refreshes, maybe the last time the timer was blocked)
Nstimer *timer =[nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (CHANGEUI) UserInfo:nil Repeats:yes];
[[Nsrunloop Currentrunloop] Addtimer:timer formode:nsrunloopcommonmodes];
Above: block that calls a timer every second
Use Note: 1. This runloop operating mode, the recommended use of Nsrunloopcommonmodes, using Nsdefaultrunloopmode, on the screen with UI updates (such as ScrollView sliding), Runloop will be switched to other modes (Nseventtrackingrunloopmode), and the timer will not be executed at this time. The nsrunloopcommonmodes contains the Nseventtrackingrunloopmode,timer, which is still executed.
2. When you are not using this timer, you need to manually call the Invalidate method to release the timer and reset it to nil. Because when you create a timer, the target is retain by default, and if you do not release the Timer,target object manually it will cause a memory leak.
3. The main thread is turned on, and the callback's selector is executed on the main thread by default. If you want multi-threaded execution of a timer, you need to manually open a nsthread, it is worth the main: Because the runloop of the child thread is turned off by default, you need to manually turn on the runloop of the child thread, otherwise the timer will never be executed
Create and execute a new thread
Nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (newthread) Object:nil];
[Thread start];
Turn on timer on new thread
-(void) Opentimer
@autoreleasepool
{
Adding a timer to the current run loop, the mode is the default Nsdefaultrunloopmode
[Nstimer scheduledtimerwithtimeinterval:2.0 target:self selector: @selector (timer_callback) Userinfo:nil Repeats:YES ];
Start execution of the new thread's run Loop
[[Nsrunloop Currentrunloop] run];
}
}
2.CADisplayLink:
The value of the Frameinterval:nsinteger type, used to set the interval for how many frames are called once selector method, the default value is 1, which is called once per frame.
The Cftimeinterval value of Duration:readonly, which represents the interval between two screen refreshes. It is important to note that this property will not be assigned until the target's selector is first called. The selector call interval is calculated by the call interval = Duration * Frameinterval (the number of times a method is called to refresh the frame) (the time required to refresh each frame)
The use of Cadisplaylink is similar to a timer, which is not described in detail here. Friends can refer to the following link.
http://blog.csdn.net/ch_soft/article/details/9408855
3.GCD Implementation Timer
The timer in GCD should be the most flexible and multi-threaded. The timer in GCD is achieved by dispatch source.
Recently used GCD timer to do a verification code to send an example. The entire use process is divided into five steps, the code is as follows:
__block Nsinteger time = 59; Countdown time
1. Create a global concurrent queue
dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
2. Create a timer to add to the queue
dispatch_source_t _timer = dispatch_source_create (dispatch_source_type_timer, 0, 0, queue);
3. Set the timer execution period, the last parameter indicates the delay seconds is 0, as accurate as possible
Dispatch_source_set_timer (_timer,dispatch_walltime (NULL, 0), 1.0*nsec_per_sec, 0);
4. Set block for timer cycle execution
Dispatch_source_set_event_handler (_timer, ^{
if (time <= 0) {//Countdown end, turn off timer, restore UI
Dispatch_source_cancel (_timer);
Dispatch_async (Dispatch_get_main_queue (), ^{
Set the style of a button
[Self.getmsgbtn settitle:@ "Get Verification Code" Forstate:uicontrolstatenormal];
[Self.getmsgbtn Settitlecolor:zgcolor (111, 255) Forstate:uicontrolstatenormal];
[Self.getmsgbtn Setbackgroundcolor:[uicolor Whitecolor];
Self.getMsgBtn.titleLabel.font = [Uifont systemfontofsize:14.0];
self.getMsgBtn.userInteractionEnabled = YES;
});
}else{//Otherwise continue timing
int leftseconds = time% 60;
Dispatch_async (Dispatch_get_main_queue (), ^{
Set button to display the read seconds effect
[Self.getmsgbtn settitle:[nsstring stringwithformat:@ "re-send after%d seconds", Leftseconds] forstate:uicontrolstatenormal];
[Self.getmsgbtn Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];
[Self.getmsgbtn Setbackgroundcolor:[uicolor Lightgraycolor];
Self.getMsgBtn.titleLabel.font = [Uifont systemfontofsize:13.0];
self.getMsgBtn.userInteractionEnabled = NO;
});
time--;
}
});
5. Start the timer
Dispatch_resume (_timer);
Use note: Because the dispatch_source_t is paused by default after it is created, it needs to be opened manually before it is executed.
In addition, there is a need in the actual development: waiting for Reload to finish Flushing TableView, and then do some operations
To delay the program to reloaddata end in action, you can use the following methods
Method 1:
[Self.tableview Reloaddata];
[Self.tableview layoutifneeded];
Refresh Complete
.......
Layoutifneeded will force redraw and wait for completion.
Method 2:
[OBJC] View plain copy
[Self.tableview Reloaddata];
Dispatch_sync (Dispatch_get_main_queue (), ^{
Refresh Complete
});
The reloaddata is executed in the primary queue, and Dispatch_get_main_queue waits for the opportunity until the home row is idle.
Research on timer delay of iOS development