您好,登錄后才能下訂單哦!
創(chuàng)建PWTimerButton 繼承與UIButton
添加成員方法和屬性
頭文件如下:
@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)了,哎,自己粗心大意了。。。
免責(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)容。