溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

自定義計(jì)時(shí)器UIButton

發(fā)布時(shí)間:2020-07-10 05:46:39 來(lái)源:網(wǎng)絡(luò) 閱讀:304 作者:一念之間888 欄目:開(kāi)發(fā)技術(shù)
  1. 創(chuàng)建PWTimerButton 繼承與UIButton

  2. 添加成員方法和屬性

  3. 頭文件如下:

@interface PWTimerButton : UIButton

{

    NSInteger _timerInterval;

    NSTimer *_timer;

    NSString *_formatString;

}


- (void)setFormatString:(NSString *)formatString;


- (void)startTimer:(NSInteger)interval;


- (void)stopTimer;


4.源文件如下:

- (void)startTimer:(NSInteger)interval

{

    if (_timer) {

        [_timer invalidate];

        _timer = nil;

    }

    if (interval > 0) {

        _timerInterval = interval;

        self.enabled = NO;

        [self updateButtonState];

        

        _timer = [NSTimer scheduledTimerWithTimeInterval:1

                                                  target:self

                                                selector:@selector(timeOut:)

                                                userInfo:nil

                                                 repeats:YES];

    }

    

}


- (void)stopTimer

{

    if (_timer) {

        [_timer invalidate];

        _timer = nil;

        _timerInterval = 0;

    }

    [self updateButtonState];

}


- (void)timeOut:(NSTimer *)timer

{

    _timerInterval--;

    if (_timerInterval > 0) {

    }

    else {

        [self stopTimer];

    }

    [self updateButtonState];

}


- (void)setFormatString:(NSString *)formatString

{

    _formatString = formatString;

}


- (void)updateButtonState

{

    if (_timerInterval > 0) {

        [self setTitle:[NSString stringWithFormat:@"%d", _timerInterval] forState:UIControlStateDisabled];

    }

    else {

        [self setTitle:@"驗(yàn)證" forState:UIControlStateNormal];

        self.enabled = YES;

    }

}

標(biāo)記***的為重點(diǎn)知識(shí),設(shè)置按鈕的什么狀態(tài)就要再什么狀態(tài)下更改按鈕的文字,顏色或者背景圖片等。

調(diào)了n長(zhǎng)時(shí)間終于調(diào)試出來(lái)了,哎,自己粗心大意了。。。



向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI