带动画的发送验证码按钮
2016-03-19 21:38
260 查看
效果见http://my.oschina.net/codeismygirl/blog/643577?fromerr=cxmofezh
自定义IFSButton继承UIView
#import "IFSButton.h"
#import "MMMaterialDesignSpinner.h"
#define scale 1.2
#define originCornerRadius 3
#define w (self.frame.size.width)
#define h (self.frame.size.height)
#define btnW (w / scale)
#define btnH (h / scale)
@interface IFSButton ()
@property (weak,
nonatomic) UIButton *button;
@property (nonatomic,weak)UIView *bgView;
@property (nonatomic,weak)MMMaterialDesignSpinner
*spinner;
@property (nonatomic,weak)UILabel *textLabel;
@property (nonatomic,strong)NSTimer *timer;
@end
@implementation IFSButton
- (void)awakeFromNib {
[superawakeFromNib];
UIButton *button = [[UIButtonalloc]init];
self.button = button;
[self addSubview:self.button];
[self.buttonaddTarget:selfaction:@selector(onClick)forControlEvents:UIControlEventTouchUpInside];
UIView *bgView = [[UIViewalloc]init];
self.bgView = bgView;
self.bgView.backgroundColor
= [UIColorcolorWithRed:1.0green:0.7blue:0.3alpha:1.0];
[self.buttonaddSubview:self.bgView];
self.bgView.layer.cornerRadius
= originCornerRadius;
self.bgView.userInteractionEnabled =NO;
MMMaterialDesignSpinner *spinner = [[MMMaterialDesignSpinneralloc]init];
self.spinner = spinner;
[self.buttonaddSubview:self.spinner];
self.spinner.lineWidth =2;
self.spinner.tintColor = [UIColorwhiteColor];
self.spinner.hidesWhenStopped =YES;
UILabel *textLabel = [[UILabelalloc]init];
self.textLabel = textLabel;
[self.buttonaddSubview:self.textLabel];
self.textLabel.text =@"发送验证码";
self.textLabel.textColor = [UIColorwhiteColor];
self.textLabel.font
= [UIFont
systemFontOfSize:15];
self.textLabel.textAlignment
= NSTextAlignmentCenter;
}
- (void)onClick {
self.button.userInteractionEnabled =NO;
[selfstartSend];
}
- (void)layoutSubviews {
[superlayoutSubviews];
self.button.bounds =CGRectMake(0,0,btnW,
btnH);
self.button.center =CGPointMake(w *0.5,h
* 0.5);
self.bgView.frame =self.button.bounds;
self.spinner.bounds =CGRectMake(0,0,btnH,
btnH);
self.spinner.center =CGPointMake(btnW *0.5,btnH
*0.5);
self.textLabel.frame =self.button.bounds;
}
- (void)startSend {
CABasicAnimation *anim = [[CABasicAnimationalloc]init];
anim.keyPath =@"cornerRadius";
anim.fromValue =@(originCornerRadius);
anim.toValue =
@(btnH * scale *0.5);
anim.duration =
0.3;
[self.bgView.layeraddAnimation:animforKey:nil];
self.bgView.layer.cornerRadius
= [anim.toValue doubleValue];
[UIViewanimateWithDuration:0.3delay:0usingSpringWithDamping:0.6initialSpringVelocity:6options:UIViewAnimationOptionCurveLinearanimations:^{
self.bgView.layer.bounds =CGRectMake(0,0,btnW
* scale,btnH *
scale);
} completion:^(BOOL finished) {
[UIViewanimateWithDuration:0.3animations:^{
self.textLabel.text =nil;
self.bgView.layer.bounds =CGRectMake(0,0,btnH
* scale,btnH *
scale);
} completion:^(BOOL finished) {
[self.spinnerstartAnimating];
[self
addTimer];
}];
}];
}
- (void)addTimer {
self.timer = [NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(updateTimer)userInfo:nilrepeats:YES];
[[NSRunLoop
mainRunLoop]
addTimer:self.timerforMode:NSRunLoopCommonModes];
[self.timerfire];
}
- (void)updateTimer {
static int n =20;
if (n > 0) {
self.textLabel.text = [NSStringstringWithFormat:@"%d",
n--];
} else {
self.textLabel.text =nil;
[self.spinnerstopAnimating];
[self removeTimer];
[self endSend];
n = 20;
}
}
- (void)removeTimer {
[self.timerinvalidate];
self.timer =nil;
}
- (void)endSend {
[UIViewanimateWithDuration:0.3animations:^{
self.bgView.layer.bounds =CGRectMake(0,0,btnW
* scale,btnH *
scale);
} completion:^(BOOL finished) {
self.textLabel.text =@"发送验证码";
CABasicAnimation *anim = [[CABasicAnimationalloc]init];
anim.keyPath =
@"cornerRadius";
anim.fromValue =
@(btnH * scale *0.5);
anim.toValue =@(originCornerRadius);
anim.duration =
0.3;
[self.bgView.layeraddAnimation:animforKey:nil];
self.bgView.layer.cornerRadius
= [anim.toValue doubleValue];
[UIViewanimateWithDuration:0.3delay:0usingSpringWithDamping:0.6initialSpringVelocity:6options:UIViewAnimationOptionCurveLinearanimations:^{
self.bgView.layer.bounds =CGRectMake(0,0,btnW,
btnH);
} completion:^(BOOL finished) {
self.button.userInteractionEnabled =YES;
}];
}];
}
@end
自定义IFSButton继承UIView
#import "IFSButton.h"
#import "MMMaterialDesignSpinner.h"
#define scale 1.2
#define originCornerRadius 3
#define w (self.frame.size.width)
#define h (self.frame.size.height)
#define btnW (w / scale)
#define btnH (h / scale)
@interface IFSButton ()
@property (weak,
nonatomic) UIButton *button;
@property (nonatomic,weak)UIView *bgView;
@property (nonatomic,weak)MMMaterialDesignSpinner
*spinner;
@property (nonatomic,weak)UILabel *textLabel;
@property (nonatomic,strong)NSTimer *timer;
@end
@implementation IFSButton
- (void)awakeFromNib {
[superawakeFromNib];
UIButton *button = [[UIButtonalloc]init];
self.button = button;
[self addSubview:self.button];
[self.buttonaddTarget:selfaction:@selector(onClick)forControlEvents:UIControlEventTouchUpInside];
UIView *bgView = [[UIViewalloc]init];
self.bgView = bgView;
self.bgView.backgroundColor
= [UIColorcolorWithRed:1.0green:0.7blue:0.3alpha:1.0];
[self.buttonaddSubview:self.bgView];
self.bgView.layer.cornerRadius
= originCornerRadius;
self.bgView.userInteractionEnabled =NO;
MMMaterialDesignSpinner *spinner = [[MMMaterialDesignSpinneralloc]init];
self.spinner = spinner;
[self.buttonaddSubview:self.spinner];
self.spinner.lineWidth =2;
self.spinner.tintColor = [UIColorwhiteColor];
self.spinner.hidesWhenStopped =YES;
UILabel *textLabel = [[UILabelalloc]init];
self.textLabel = textLabel;
[self.buttonaddSubview:self.textLabel];
self.textLabel.text =@"发送验证码";
self.textLabel.textColor = [UIColorwhiteColor];
self.textLabel.font
= [UIFont
systemFontOfSize:15];
self.textLabel.textAlignment
= NSTextAlignmentCenter;
}
- (void)onClick {
self.button.userInteractionEnabled =NO;
[selfstartSend];
}
- (void)layoutSubviews {
[superlayoutSubviews];
self.button.bounds =CGRectMake(0,0,btnW,
btnH);
self.button.center =CGPointMake(w *0.5,h
* 0.5);
self.bgView.frame =self.button.bounds;
self.spinner.bounds =CGRectMake(0,0,btnH,
btnH);
self.spinner.center =CGPointMake(btnW *0.5,btnH
*0.5);
self.textLabel.frame =self.button.bounds;
}
- (void)startSend {
CABasicAnimation *anim = [[CABasicAnimationalloc]init];
anim.keyPath =@"cornerRadius";
anim.fromValue =@(originCornerRadius);
anim.toValue =
@(btnH * scale *0.5);
anim.duration =
0.3;
[self.bgView.layeraddAnimation:animforKey:nil];
self.bgView.layer.cornerRadius
= [anim.toValue doubleValue];
[UIViewanimateWithDuration:0.3delay:0usingSpringWithDamping:0.6initialSpringVelocity:6options:UIViewAnimationOptionCurveLinearanimations:^{
self.bgView.layer.bounds =CGRectMake(0,0,btnW
* scale,btnH *
scale);
} completion:^(BOOL finished) {
[UIViewanimateWithDuration:0.3animations:^{
self.textLabel.text =nil;
self.bgView.layer.bounds =CGRectMake(0,0,btnH
* scale,btnH *
scale);
} completion:^(BOOL finished) {
[self.spinnerstartAnimating];
[self
addTimer];
}];
}];
}
- (void)addTimer {
self.timer = [NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(updateTimer)userInfo:nilrepeats:YES];
[[NSRunLoop
mainRunLoop]
addTimer:self.timerforMode:NSRunLoopCommonModes];
[self.timerfire];
}
- (void)updateTimer {
static int n =20;
if (n > 0) {
self.textLabel.text = [NSStringstringWithFormat:@"%d",
n--];
} else {
self.textLabel.text =nil;
[self.spinnerstopAnimating];
[self removeTimer];
[self endSend];
n = 20;
}
}
- (void)removeTimer {
[self.timerinvalidate];
self.timer =nil;
}
- (void)endSend {
[UIViewanimateWithDuration:0.3animations:^{
self.bgView.layer.bounds =CGRectMake(0,0,btnW
* scale,btnH *
scale);
} completion:^(BOOL finished) {
self.textLabel.text =@"发送验证码";
CABasicAnimation *anim = [[CABasicAnimationalloc]init];
anim.keyPath =
@"cornerRadius";
anim.fromValue =
@(btnH * scale *0.5);
anim.toValue =@(originCornerRadius);
anim.duration =
0.3;
[self.bgView.layeraddAnimation:animforKey:nil];
self.bgView.layer.cornerRadius
= [anim.toValue doubleValue];
[UIViewanimateWithDuration:0.3delay:0usingSpringWithDamping:0.6initialSpringVelocity:6options:UIViewAnimationOptionCurveLinearanimations:^{
self.bgView.layer.bounds =CGRectMake(0,0,btnW,
btnH);
} completion:^(BOOL finished) {
self.button.userInteractionEnabled =YES;
}];
}];
}
@end
相关文章推荐
- unity c# 泛型委托
- fl2440内核移植(五)——USB驱动移植
- Power Strings (POJ_2406) KMP + 循环节
- unity c# 标准事件模式
- 用visionPro初始化相机时提示Buffer Retrieve Failed Error的解决方法
- 关于web前端的初步理解
- 【转】Java 中的 i = i++
- 学习进度博客二
- LeetCode 274. H-Index
- PAT 乙级 1014. 福尔摩斯的约会
- unity c# 事件
- c# 委托
- 蓝桥杯—— 基础练习之Sine之舞
- {Leetcode} 1。TwoSum
- html例题——简历
- 3月19
- Osmocom-BB MOTO C118硬刷
- Linux 运行级别
- ZOJ 3596 Digit Number【状态压缩】【BFS】
- Android图像处理之Paint