IOS countdown NSTimer
There may be some countdown points in the project
For example, when mobile phone verification is performed, the Verification Code generally has a time limit. At this time, a countdown is required for entering the verification code.
The specific implementation method is to use NSTimer that comes with iOS
Code on
Create
Int secondsCountDown; // total countdown duration NSTimer * countDownTimer; UILabel * labelText;
Then implement
// Create a UILabel and add it to the current view labelText = [[UILabel alloc] initWithFrame: CGRectMake (10,120,120, 36)]; [self. view addSubview: labelText]; // sets the total countdown duration secondsCountDown = 60; // 60 seconds countdown // starts the countdown countDownTimer = [nst1_scheduledtimerwithtimeinterval: 1 target: self selector: @ selector (timeFireMethod) userInfo: nil repeats: YES]; // After the start countdown, the method timeFireMethod is called every second. // you can specify the time when the countdown is displayed as labelText. text = [NSString stringWithFormat: @ % d, secondsCountDown];
Implementation Method per second
-(Void) timeFireMethod {// countdown-1 secondsCountDown --; // modify the actual content labelText of the Countdown tag. text = [NSString stringWithFormat: @ % d, secondsCountDown]; // The operation required when the countdown reaches 0. For example, if (secondsCountDown = 0) cannot be submitted when the verification code expires) {[countDownTimer invalidate]; [labelText removeFromSuperview];}
It has been roughly implemented. If you have any questions, continue to communicate.