转载自:http://www.jianshu.com/p/024dd2d6e6e6#

Update: Xcode 8.2.1 Swift 3

先介绍一下 属性观测器(Property Observers)

属性观察器监控和响应属性值的变化,每次属性被设置值的时候都会调用属性观察器,甚至新的值和现在的值相同的时候也不例外。

可以为属性添加如下的一个或全部观察器:

  • willSet在新的值被设置之前调用
  • didSet在新的值被设置之后立即调用

摘录来自: 极客学院. “The Swift Programming Language 中文版”。 iBooks.

接下来开始我们的教程,先展示一下最终效果:


SMSScreenshot.gif

首先声明一个发送按钮:

var sendButton: UIButton!

viewDidLoad方法中给发送按钮添加属性:

override func viewDidLoad() {
super.viewDidLoad() sendButton = UIButton()
sendButton.frame = CGRect(x: 40, y: 100, width: view.bounds.width - 80, height: 40)
sendButton.backgroundColor = UIColor.red
sendButton.setTitleColor(UIColor.white, for: .normal)
sendButton.setTitle("获取验证码", for: .normal)
sendButton.addTarget(self, action: #selector(ViewController.sendButtonClick(_:)), for: .touchUpInside) self.view.addSubview(sendButton)
}

接下来声明一个变量remainingSeconds代表当前倒计时剩余的秒数:

var remainingSeconds = 0

我们给remainingSeconds添加一个willSet方法,这个方法会在remainingSeconds的值将要变化的时候调用,并把值传递给参数newValue:

var remainingSeconds: Int = 0 {
willSet {
sendButton.setTitle("验证码已发送(\(newValue)秒后重新获取)", forState: .normal) if newValue <= 0 {
sendButton.setTitle("重新获取验证码", forState: .normal)
isCounting = false
}
}
}

remainingSeconds变化时更新sendButton的显示文本。

倒计时的功能我们用Timer实现,先声明一个Timer实例:

var countdownTimer: Timer?

然后我们声明一个变量来开启和关闭倒计时:

var isCounting = false {
willSet {
if newValue {
countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.updateTime(_:)), userInfo: nil, repeats: true) remainingSeconds = 10
sendButton.backgroundColor = UIColor.gray
} else {
countdownTimer?.invalidate()
countdownTimer = nil sendButton.backgroundColor = UIColor.red
} sendButton.enabled = !newValue
}
}

同样,我们给isCounting添加一个willSet方法,当isCountingnewValuetrue时,我们通过调用Timer的类方法
scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:创建并启动刚才声明的countdownTimer实例,这个实例每一秒钟调用一次updateTime:方法:

func updateTime(timer: Timer) {
// 计时开始时,逐秒减少remainingSeconds的值
remainingSeconds -= 1
}

isCountingnewValuefalse时,我们停止countdownTimer并将countdownTimer设置为nil

此外我们还设置了倒计时的时间(这里为了演示时间设置为5秒)和发送按钮在不同isCounting状态下的样式(这里调整了背景色)和是否可点击。

最后实现sendButtonClick:方法,这个方法在点击sendButton时调用:

 func sendButtonClick(sender: UIButton) {
// 启动倒计时
isCounting = true
}

完成!

Github地址:
https://github.com/GuiminChu/JianshuExample/tree/master/SMSSendButtonExample

最新文章

  1. 【转】string.Format对C#字符串格式化
  2. 倒计时的js实现 倒计时 js Jquery
  3. 【BZOJ-1597】土地购买 DP + 斜率优化
  4. c++作用域运算符---7
  5. css学习笔记(5)
  6. jQuery MiniUI 开发指南+API组件参考手册
  7. iOS开发小技巧--高斯模糊框架的应用
  8. Qt5 QTableWidget设置列表自动适应列宽
  9. 第三百四十八天 how can I 坚持
  10. 应用越来越广泛的css伪类
  11. 删除mysql的root用户恢复方法
  12. 团体程序设计天梯赛-练习集L1-012. 计算指数
  13. Powershell变量的幕后管理
  14. OAuth做webapi认证
  15. C#中易混淆的知识点
  16. hbase-读操作
  17. 探秘小程序(7):view组件
  18. Python 算法实现
  19. org.hibernate.TransientObjectException异常
  20. python之模块contextlib 加强with语句而存在

热门文章

  1. 难题解决:Mycat数据库中间件+Mybatis批量插入数据并返回行记录的所有主键ID
  2. .net core 3.0 Signalr - 09 待改进&amp;交流
  3. Redis Cluster 原理相关说明
  4. MongoDB 学习笔记之 查询表达式
  5. Nginx internal 指令限制访问图片资源文件
  6. Http协议Content-Length详解
  7. Axure实现百度登录页面(一)
  8. orm加强版
  9. eclipse 的使用
  10. centos7升级openssl、openssh常见问题及解决方法