在项目中经常会使用到获取验证码的功能:注册页面,找回密码,绑定手机,修改余额的支付密码,余额支付页面等,这样重复在每个页面使用会照成代码的冗余以及增加不必要的工作量,下面就封装了这样一个只需一行代码获取验证码的功能~废话不多说,先看图~~喜欢就给个❤哦!后续还会不断更新一些实际开发中常用的功能~
实现代码:
- (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,希望我的文章对你有帮助。