带动画的发送验证码按钮

效果见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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值