iOS 一行代碼實現發送驗證碼倒計時!

Qinz


? ? 在項目中經常會使用到獲取驗證碼的功能:注冊頁面,找回密碼,綁定手機,修改余額的支付密碼,余額支付頁面等,這樣重復在每個頁面使用會照成代碼的冗余以及增加不必要的工作量,下面就封裝了這樣一個只需一行代碼獲取驗證碼的功能~廢話不多說,先看圖~~喜歡就給個?哦!后續還會不斷更新一些實際開發中常用的功能~



實現代碼:

- (IBAction)clickBtn:(MobileCodeButton *)sender {

//btn的發光效果

sender.showsTouchWhenHighlighted=YES;

//只需用到的一行代碼:需要注意的時設置btn的Type為Custom(不然會有卡頓現象)。

[sender sendCodeToMobile:@"18688888888" complete:^(BOOL success, NSString *msg) {

}];

}


MobileCodeButton.h


#import@interface MobileCodeButton : UIButton

/**

*? 發送驗證碼

*

*? @param mobile? 接收驗證碼的手機

*? @param complete 驗證的回調,當succes為yes時,表示驗證手機通過,開始發送獲取驗證碼的請求,同時倒計時,success為no時,返回msg錯誤信息

*/

- (void) sendCodeToMobile:(NSString *) mobile complete:(void (^)(BOOL success,NSString *msg)) complete;

@end


MobileCodeButton.m

核心代碼:

#import "MobileCodeButton.h"

@interface MobileCodeButton ()

@property (nonatomic, copy) NSString *mobile;

@property (nonatomic, assign) NSInteger count;

@property (nonatomic, strong) CAAnimationGroup *animation;

@end

static NSString *tableName = @"Localizable";

@implementation MobileCodeButton

- (void)sendCodeToMobile:(NSString *)mobile complete:(void (^)(BOOL success,NSString *msg))complete{

_mobile = mobile;

_count = 60;

[self sendCodeComplete:complete];

}

-(void)sendCodeComplete:(void (^)(BOOL success,NSString *msg)) complete{

if ([_mobile isEqualToString:@""]||(_mobile==nil)) {

NSString *msg = @"mobile.not.allow.empty";

complete(NO,NSLocalizedStringFromTable(msg, @"Localizable", msg));

}else{

[self performSelector:@selector(countClick) withObject:nil];

complete(YES,nil);

}

}

-(void)countClick

{

self.enabled =NO;

_count = 60;

NSString *msg = NSLocalizedStringFromTable(@"seconds", tableName, @"seconds");

[self setTitle:[NSString stringWithFormat:@"%zd%@",_count,msg] forState:UIControlStateDisabled];

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];

[self numAnimation];

}

-(void)timerFired:(NSTimer *)timer

{

if (_count !=1) {

_count -=1;

[self setTitle:[NSString stringWithFormat:@"%ld秒",_count] forState:UIControlStateDisabled];

}else{

[timer invalidate];

self.enabled = YES;

[self.titleLabel.layer removeAllAnimations];

NSString *msg = NSLocalizedStringFromTable(@"get.code", tableName, @"get.code");

[self setTitle:msg forState:UIControlStateNormal];

}

}

- (void) numAnimation{

CFTimeInterval duration = 1;

CFTimeInterval beginTime = CACurrentMediaTime();

// Scale animation

CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];

scaleAnimation.keyTimes = @[@0, @0.5, @1];

scaleAnimation.values = @[@1, @1.5, @2];

scaleAnimation.duration = duration;

// Opacity animation

CAKeyframeAnimation *opacityAnimaton = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];

opacityAnimaton.keyTimes = @[@0, @0.5, @1];

opacityAnimaton.values = @[@1, @0.5, @0];

opacityAnimaton.duration = duration;

// Animation動畫

CAAnimationGroup *animation = [CAAnimationGroup animation];

_animation = animation;

animation.animations = @[scaleAnimation, opacityAnimaton];

animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

animation.duration = duration;

animation.repeatCount = HUGE;

animation.removedOnCompletion = NO;

animation.beginTime = beginTime;

[self.titleLabel.layer addAnimation:animation forKey:@"animation"];

}

@end


我是Qinz,希望我的文章對你有幫助。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容