您的位置:首页 > 其它

带动画的发送验证码按钮

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: