The main thread always refreshes the countdown when the page is toggled in the GCD
__block int timeout = 2400; Countdown time
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), 1.0*nsec_per_sec, 0); No second execution.
Dispatch_source_set_event_handler (_timer, ^{
if (timeout<=0) {//Countdown end, close
Dispatch_source_cancel (_timer);
Dispatch_async (Dispatch_get_main_queue (), ^{
Set the interface button to display the settings according to your needs
Uialertview * alert = [[Uialertview alloc]initwithtitle:@ "Prompt" message:@ "will be delivered" Delegate:nil Cancelbuttontitle:nil otherbuttontitles:@ "OK", @ "Cancel", nil];
[Alert show];
});
}else{
int minutes = TIMEOUT/60;
NSString * Strtime = [NSString stringwithformat:@ "%d", minutes];
Dispatch_async (Dispatch_get_main_queue (), ^{
Set the interface button to display the settings according to your needs
UIView * Timeview = [[UIView alloc]initwithframe:cgrectmake (10, 140, 150, 150)];
TimeView.layer.cornerRadius = 75;
TimeView.layer.borderWidth = 4;
TimeView.layer.borderColor = [Uicolor colorwithred:0.816 green:0.820 blue:0.824alpha:1.000]. Cgcolor;
Timeview.backgroundcolor = [Uicolor Whitecolor];
[_mainview Addsubview:timeview];
UILabel * Tlabel = [[UILabel alloc]initwithframe:cgrectmake (55, 0, 60, 60)];
Tlabel.text = @ "remaining";
Tlabel.textcolor = [Uicolor blackcolor];
Tlabel.font = [Uifont systemfontofsize:20];
[Timeview Addsubview:tlabel];
UILabel * Mlabel = [[UILabel alloc]initwithframe:cgrectmake (35, 30, 100, 100)];
Mlabel.text = Strtime;
Mlabel.font = [Uifont systemfontofsize:80];
Mlabel.textcolor = [Uicolor Redcolor];
[Timeview Addsubview:mlabel];
UILabel * Llabel = [[UILabel alloc]initwithframe:cgrectmake (55, 100, 60, 60)];
Llabel.text = @ "Minutes";
Llabel.textcolor = [Uicolor Redcolor];
Llabel.font = [Uifont systemfontofsize:20];
[Timeview Addsubview:llabel];
});
timeout--;
}
});
Dispatch_resume (_timer);
#pragrm Mark---nstimer---
- int Secondscountdown; //Countdown Total Duration
- Nstimer *countdowntimer;
UILabel *labeltext;
- Create Uilabel add to Current view
- Labeltext=[[uilabel Alloc]initwithframe:cgrectmake (10, 120, 12 0, 36)] ;
- [self. View Addsubview:labeltext];
- Set the total countdown time
- Secondscountdown = 60; 60 seconds Countdown
- Start the countdown.
- Countdowntimer = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector:@selector ( Timefiremethod) userInfo: nil repeats:YES]; //The method is called once every second after the countdown starts Timefiremethod
- Set the countdown display time
- LabelText. text=[nsstring stringWithFormat:@ "%d", Secondscountdown];
- -(void) timefiremethod{
- //Countdown-1
- secondscountdown--;
- //Modify the countdown tag real content
- LabelText. text=[nsstring stringWithFormat:@ "%d", Secondscountdown];
- //When countdown to 0 o'clock, do the required actions, such as verification code expiration cannot be submitted
- if (secondscountdown==0) {
- [Countdowntimer invalidate];
- [LabelText Removefromsuperview];
- }
- }
GCD and Timer