需求
实现按钮倒计时:基于GCD的倒计时
github链接:https://github.com/ZPP506/ZPPCountdown
使用NSTimer实现倒计时的缺点
- NSTimer添加到main runloop中,模式是NSDefaultRunLoopMode,main负责所有主线程事件,例如UI界面的操作,复杂的运算,这样在同一个runloop中timer就会产生阻塞
- 模式的改变。主线程的 RunLoop 里有两个预置的 Mode:kCFRunLoopDefaultMode 和 UITrackingRunLoopMode
GCD定时器不受RunLoop约束,比NSTimer更加准时
效果图
使用方法
注意
- 按钮:创建时的类型不同,闪烁效果不同
UIButtonTypeCustom->普通
UIButtonTypeSystem ->闪烁
开始倒计时
1:
[button startTimer:60]
2:
[button startTimer:60 endComplete:nil];
取消倒计时
[button cancelTime]
安装
ZPPCountdown 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile
pod 'ZPPCountdown'
作者
张朋朋, [email protected]
许可协议
ZPPCountdown 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。