GCD and Timer

Source: Internet
Author: User

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---

    1. int Secondscountdown; //Countdown Total Duration
    2. Nstimer *countdowntimer;
    3. UILabel *labeltext;

  1. Create Uilabel add to Current view
  2. Labeltext=[[uilabel Alloc]initwithframe:cgrectmake (10, 120, 12 0, 36)]  ;
  3. [self. View Addsubview:labeltext];
  4. Set the total countdown time
  5. Secondscountdown = 60; 60 seconds Countdown
  6. Start the countdown.
  7. 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
  8. Set the countdown display time
  9. LabelText. text=[nsstring stringWithFormat:@ "%d", Secondscountdown];

  1. -(void) timefiremethod{
  2. //Countdown-1
  3. secondscountdown--;
  4. //Modify the countdown tag real content
  5. LabelText. text=[nsstring stringWithFormat:@ "%d", Secondscountdown];
  6. //When countdown to 0 o'clock, do the required actions, such as verification code expiration cannot be submitted
  7. if (secondscountdown==0) {
  8. [Countdowntimer invalidate];
  9. [LabelText Removefromsuperview];
  10. }
  11. }

GCD and 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.