簡單的“獲取驗證碼”的按鈕功能實現


最近做項目遇到一個小問題,獲取碼證碼,通常這種界面直接用xib來布局這個注冊頁面,以往用的是純代碼,然后發現了一個小bug,以前用純代碼寫的獲取驗證碼這個功能正常的,但是現在用xib拖拽的按鈕,雖然可以實現,但是文字會閃爍,這到底是為什么純代碼沒有問題,xib就出現這個問題呢?我也不知道,其實這里第一次寫這個博客,寫得有點爛,先上代碼

 

#import "ViewController.h"

@interface ViewController ()

 

/* 時間 */

@property (nonatomic, assign) int timeCount;

/* 定時器 */

@property (nonatomic, strong) NSTimer *timer;

/* 按鈕 */

@property (nonatomic, weak) UIButton *messageButton;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    

    //創建按鈕

    UIButton *btn = [[UIButton alloc] init];

    [btn addTarget:self action:@selector(sendMessageClick) forControlEvents:UIControlEventTouchUpInside];

    [btn setTitle:@"獲取碼證碼" forState:UIControlStateNormal];

    btn.frame = CGRectMake(100, 100, 200, 40);

    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    btn.backgroundColor = [UIColor redColor];

    [self.view addSubview:btn];

    self.messageButton = btn;

}

 

//偵聽

- (void)sendMessageClick

{

    self.messageButton.backgroundColor = [UIColor grayColor];

    self.messageButton.userInteractionEnabled = NO;

    //開始倒計時

    [self beginCalculationTime];

}

 

//開始倒計時

- (void)beginCalculationTime

{

    self.timeCount = 10;

    [self.messageButton setTitle:@"重新獲取(60)" forState:UIControlStateNormal];

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(refreshTime) userInfo:nil repeats:YES];

    [self.timer fire];

}

 

//timer

- (void)refreshTime

{

    self.label.text = [NSString stringWithFormat:@"重新獲取(%ld)",(long)self.timeCount];

    self.timeCount -= 1;

    [self.messageButton setTitle:[NSString stringWithFormat:@"重新獲取(%ld)",(long)self.timeCount] forState:UIControlStateNormal];

    if (self.timeCount == 0) {

        [self.messageButton setTitle:@"重新獲取" forState:UIControlStateNormal];

        [self.timer invalidate];

        self.messageButton.userInteractionEnabled = YES;

        self.messageButton.backgroundColor = [UIColor redColor];

    }

}

 

@end


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com