IOS Implementation Verification Code countdown function (i) _ios

Source: Internet
Author: User
Tags set background touch

The Verification Code Countdown button application is very common, the blog and you together to write a idcountdownbutton to achieve the verification code countdown effect. You can want to use the ordinary UIButton type button, just need to set its countdown length (if not set, the default is 60 seconds), you can easily realize click Countdownbutton Start Countdown, Countdown to the end can be clicked again.

First, the realization effect
As shown in figure

Second, the realization of ideas
1, custom a Idcountdownbutton, rewrite begintrackingwithtouch:withevent: Intercept button's Click event, Decide whether to respond and pass the button's Click event based on whether the countdown is being counted (if the countdown is in progress, clicking again will not restart the countdown)
2, is the use of Nstimer timer, timing change Idcountdownbutton title
3, if the countdown is over, cancel the timer and reply to the countdown length (so that Idcountdownbutton have the ability to start the countdown again)
4, in the Idcountdownbutton destruction, also cancels the timer
Third, the realization step
1, add the related properties
publicly owned property (public)

@interface Idcountdownbutton:uibutton
/** Verification Code Countdown hours
/@property (nonatomic, assign) Nsinteger Durationofcountdown;
@end

Private property

@interface Idcountdownbutton ()
/** Save the countdown button is not the countdown status of title
/@property (nonatomic, copy) NSString * Originaltitle;
/** Save countdown time Long
/@property (nonatomic, assign) Nsinteger Tempdurationofcountdown;
/** Timer Object * *
@property (nonatomic, strong) Nstimer *countdowntimer;
@end

2. Rewrite setter
setter for title property
1), private property originaltitle is used to hold the caption of the button before the start timer, that is, the caption of the button that the user set, usually "get the Captcha"
2, need to screen the timing process, title update change Originaltitle value

-(void) Settitle: (NSString *) title forstate: (uicontrolstate) state {
 [Super Settitle:title forstate:state];
 The change of title in the countdown process does not update Originaltitle
 if (Self.tempdurationofcountdown = = Self.durationofcountdown) {
 Self.originaltitle = title;
 }

Setter for Durationofcountdown Properties
1), set the value of Tempdurationofcountdown
2), the role of Tempdurationofcountdown: Countdown, and durationofcountdown coordination to determine whether the current Idcountdownbutton have the ability to restart the countdown

-(void) Setdurationofcountdown: (Nsinteger) Durationofcountdown {
 _durationofcountdown = Durationofcountdown;
 Self.tempdurationofcountdown = _durationofcountdown;
}

Class
1), set countdown to the default length of 60 wonderful
2), set Idcountdownbutton the default title is "Get Authentication Code"

-(Instancetype) initWithFrame: (CGRect) Frame {
 if (self = [Super Initwithframe:frame]) {
 //Set the default countdown length to 60 seconds
 Self.durationofcountdown =;
 The default caption for setting the button is "Get Authentication code"
 [self settitle:@ to get the authentication code ' forstate:uicontrolstatenormal];
 }
 return self;
}
-(Instancetype) Initwithcoder: (Nscoder *) Adecoder {
 if (self = [Super Initwithcoder:adecoder]) {
 // Set the default countdown length to 60 seconds
 Self.durationofcountdown =;
 The default caption for setting the button is "Get Authentication code"
 [self settitle:@ to get the authentication code ' forstate:uicontrolstatenormal];
 }
 return self;
}

Intercept Idcountdownbutton Click events to determine whether to start the countdown
1), if the Tempdurationofcountdown equals Durationofcountdown, the description does not start the countdown, responds and delivers Idcountdownbutton clicks the event, otherwise, does not respond and does not pass.

-(BOOL) Begintrackingwithtouch: (Uitouch *) Touch withevent: (uievent *) event {
 //If the countdown, does not respond to click event
 ( Self.tempdurationofcountdown!= self.durationofcountdown) {return
 NO;
 }
 If the countdown is not started, respond and pass the Click event, start the Countdown
 [self startcountdown];
 return [Super Begintrackingwithtouch:touch withevent:event];
}

Countdown
1), create timer, start countdown

-(void) Startcountdown {
 //create timer
 Self.countdowntimer = [Nstimer timerwithtimeinterval:1 target:self Selector: @selector (updateidcountdownbuttontitle) Userinfo:nil Repeats:yes];
 Add the timer to the current runloop (automatically turn on the timer)
 [[Nsrunloop Currentrunloop] AddTimer:self.countDownTimer formode: Nsrunloopcommonmodes];
}

2), update Idcountdownbutton title for the countdown remaining time

-(void) Updateidcountdownbuttontitle {
 if (Self.tempdurationofcountdown = 0) {
 // Set Idcountdownbutton title to start the countdown before the title
 [self setTitle:self.originalTitle forstate:uicontrolstatenormal];
 Restore Idcountdownbutton ability to start countdown
 Self.tempdurationofcountdown = Self.durationofcountdown;
 [Self.countdowntimer invalidate];
 } else {
 //Set Idcountdownbutton title to the time remaining for the current Countdown
 [self settitle:[nsstring stringwithformat:@ "%zd seconds", self.tempdurationofcountdown--] forstate:uicontrolstatenormal];
 }

3), removal timer

-(void) dealloc {
 [self.countdowntimer invalidate];
}

Using the sample
1), add Vertificationcodeidcountdownbutton property

@interface Viewcontroller ()
/** Verification Code Countdown button
/@property (nonatomic, strong) Idcountdownbutton * Vertificationcodeidcountdownbutton;
@end

2), create Vertificationcodeidcountdownbutton, and make related settings

-(void) viewdidload {[Super viewdidload]; Create Vertificationcodeidcountdownbutton Self.vertificationcodeidcountdownbutton = [[Idcountdownbutton alloc]
 Initwithframe:cgrectmake (160, 204, 120, 44)]; Add Click event [Self.vertificationcodeidcountdownbutton addtarget:self action: @selector (
 Vertificationcodeidcountdownbuttonclick:) forcontrolevents:uicontroleventtouchupinside]; Set caption-related properties [Self.vertificationcodeidcountdownbutton settitlecolor:[uicolor Whitecolor] forstate:
 UIControlStateNormal];
 [Self.vertificationcodeidcountdownbutton settitle:@ "Obtain authentication code" Forstate:uicontrolstatenormal]; Set background picture [Self.vertificationcodeidcountdownbutton setbackgroundimage:[uiimage imagenamed:@ "RedButton"] forState:
 UIControlStateNormal];
 Set countdown time Long Self.vertificationCodeIDCountDownButton.durationOfCountDown = 10; Add Vertificationcodeidcountdownbutton to the controller's view [Self.view AddSubview:self.vertificationCodeIDCountDownButton]
; }

3), the implementation of Click event Trigger action

-(void) Vertificationcodeidcountdownbuttonclick: (UIButton *) button {
 //TODO: Invoke Server interface, get authentication code
}

Iv. about AppIcon
You need to follow these rules when adding appicon
1, named, with the beginning of the icon (first letter capital), keep up with @2x/@3x, as shown:

2), size, must be set according to the requirements of the size, as shown

3), the figure shown in the 60pt corresponding picture size is
2x:120px X 120px
3x:180px X 180px

The above is the entire content of this article, we can also combine the second iOS to implement the Verification Code countdown function (ii) to learn, I hope to help you learn.

Related Article

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.