在开始之前,我们先来了解一个概念 属性观测器(property observers):
属性观察器监控和响应属性值的变化,每次属性被设置值的时候都会调用属性观察器,甚至新的值和现在的值相同的时候也不例外。
可以为属性添加如下的一个或全部观察器:
接下来开始我们的教程,先展示一下最终效果:
首先声明一个发送按钮:
sendbutton = uibutton()
sendbutton.frame = cgrect(x: 40, y: 100, width: view.bounds.width - 80, height: 40)
sendbutton.backgroundcolor = uicolor.redcolor()
sendbutton.settitlecolor(uicolor.whitecolor(), forstate: .normal)
sendbutton.settitle("获取验证码", forstate: .normal)
sendbutton.addtarget(self, action: "sendbuttonclick:", forcontrolevents: .touchupinside)
self.view.addsubview(sendbutton)
}
if newvalue <= 0 {
sendbutton.settitle("重新获取验证码", forstate: .normal)
iscounting = false
}
}
}
倒计时的功能我们用nstimer实现,先声明一个nstimer实例:
remainingseconds = 10
sendbutton.backgroundcolor = uicolor.graycolor()
} else {
countdowntimer?.invalidate()
countdowntimer = nil
sendbutton.backgroundcolor = uicolor.redcolor()
}
sendbutton.enabled = !newvalue
}
}
此外我们还设置了倒计时的时间(这里为了演示时间设置为5秒)和发送按钮在不同iscounting状态下的样式(这里调整了背景色)和是否可点击。
最后实现sendbuttonclick:方法,这个方法在点击sendbutton时调用:
如对本文有疑问, 点击进行留言回复!!
牛客编程巅峰赛S1第6场 - 黄金&钻石&王者题解
纵横字谜的答案 Crossword Answers, ACM/ICPC World Finals 1994, UVa232
HDU - 5880 Family View (AC自动机修改母串)
iOS14Beta3续航怎么样 iOS14Beta3续航能力介绍
iOS14Beta3稳定性怎么样 iOS14Beta3升级建议介绍
网友评论