CountdownLabel
具有变形动画的简单倒计时 UILabel,并附带一些有用的功能。
特性
- 简单创建
- 通过属性和代理轻松获取倒计时的状态。
- 插入一些函数和完成代码
- 样式更改与 UILabel 类似
- 变形动画来自 LTMorphingLabel。
- XCTest 断言
版本 vs Swift 版本。
以下表格显示了您应根据 Swift 版本使用哪些版本。
Swift 版本 | 版本 |
---|---|
4.2 | >= 4.0 |
4.0, 4.1 | >= 3.0 |
3.X | >= 2.0 |
2.3 | 1.3 |
用法
只需两行代码。
// from current Date, after 30 minutes.
let countdownLabel = CountdownLabel(frame: frame, minutes: 30) // you can use NSDate as well
countdownLabel.start()
形变示例
使用 animationType
。这些效果来自 LTMorphingLabel。
let countdownLabel = CountdownLabel(frame: CGRectZero, time: 60*60)
countdownLabel.animationType = .Pixelate
countdownLabel.start()
形变效果 | 示例 |
---|---|
烧毁。 | ![]() |
蒸发。 | ![]() |
下落。 | ![]() |
像素化。 | ![]() |
缩放。 | ![]() |
闪光。 | ![]() |
样式
您可以像往常一样将其作为 UILabel 属性直接分配。
countdownLabel.textColor = .orangeColor()
countdownLabel.font = UIFont(name:"Courier", size:UIFont.labelFontSize())
countdownLabel.start()
获取计时器状态
有一些属性可以读取状态。
countdownLabel.timeCounted // timer that has been counted
countdownLabel.timeRemaining // timer's remaining
// example
@IBAction func getTimerCounted(sender: UIButton) {
debugPrint("\(countdownLabel.timeCounted)")
}
@IBAction func getTimerRemain(sender: UIButton) {
debugPrint("\(countdownLabel.timeRemaining)")
}
控制倒计时
您可以暂停、开始或更改时间。
// check if pause or not
if countdownLabel.isPaused {
// timer start
countdownLabel.start()
} else {
// timer pause
countdownLabel.pause()
}
// -2 minutes for ending
@IBAction func minus(btn: UIButton) {
countdownLabel.addTime(-2)
}
// +2 minutes for ending
@IBAction func plus(btn: UIButton) {
countdownLabel.addTime(2)
}
插入函数
使用 then
函数或 delegate
,您可以在任何您喜欢的位置设置您的函数。
// then property
countdownLabel.then(10) { [unowned self] in
self.countdownLabel.animationType = .Pixelate
self.countdownLabel.textColor = .greenColor()
}
countdownLabel.then(5) { [unowned self] in
self.countdownLabel.animationType = .Sparkle
self.countdownLabel.textColor = .yellowColor()
}
countdownLabel.start() {
self.countdownLabel.textColor = .whiteColor()
}
// delegate
func countingAt(timeCounted timeCounted: NSTimeInterval, timeRemaining: NSTimeInterval) {
switch timeRemaining {
case 10:
self.countdownLabel6.animationType = .Pixelate
self.countdownLabel6.textColor = .greenColor()
case 5:
self.countdownLabel6.animationType = .Sparkle
self.countdownLabel6.textColor = .yellowColor()
default:
break
}
}
func countdownFinished() {
self.countdownLabel.textColor = .whiteColor()
}
属性文本
也可以设置属性文本。注意:但是形变动画将被禁用。
countdownLabel.setCountDownTime(30)
countdownLabel.timeFormat = "ss"
countdownLabel.countdownAttributedText = CountdownAttributedText(text: "timer HERE in text", replacement: "HERE")
countdownLabel.start()
格式
不要指定超过24小时,否则会得到错误的格式。CountdownLabel使用默认格式00:00:00 (HH:mm:ss)
。如果您想使用其他格式,您可以按照以下方式设置您的时时间格式。
countdownLabel.timeFormat = @"mm:ss"
已计划
您可以设置计划倒计时
// after 10 minutes will start a countdown from 20.
let fromDate = NSDate().dateByAddingTimeInterval(10)
let targetDate = fromDate.dateByAddingTimeInterval(20)
let countdownLabel = CountdownLabel(frame: CGRectZero, fromDate: fromDate, targetDate: targetDate)
countdownLabel.start()
检查状态
有一些公共属性非常有用。
countdownLabel.isCounting // check timer is counting now
countdownLabel.isPaused // check timer was stopped
countdownLabel.isFinished // check timer has ended
countdownLabel.morphingEnabled // check morphing is enabled
要求
- iOS 9.0+
- Swift 2.3+
- ARC
##安装
####CocoaPods CocoaPods上有可用的CocoaPods。只需将以下内容添加到您的项目Podfile中。
pod 'CountdownLabel'
use_frameworks!
####Carthage 要使用Carthage集成到您的Xcode项目中,请将其指定在您的Cartfile中。
github "suzuki-0000/CountdownLabel"
灵感来源
- LTMorphingLabel是创建此项目的动力。
- 从MZTimerLabel许多参考中提取。
许可协议
在MIT许可协议下可用。有关更多信息,请参阅LICENSE文件。