-1
我想在我的应用中使用“时间”。我有一个“每秒收入”的价值,然后我想要一个标签来告诉我总共有多少收入。 所以它应该是这样的:收入/秒*已经过去的秒数=总收入。我希望这个数字一直在变化。 我该怎么做?在Xcode中利用时间
我想在我的应用中使用“时间”。我有一个“每秒收入”的价值,然后我想要一个标签来告诉我总共有多少收入。 所以它应该是这样的:收入/秒*已经过去的秒数=总收入。我希望这个数字一直在变化。 我该怎么做?在Xcode中利用时间
你需要查找NSTimer类,它有一个静态方法来创建无限重复的à定时器。
你将它设置为重复每一秒,并更新为新值的标签,你去好:)
无法从我的手机现在链接,但任何人都随意编辑我的回复文档链接
var timer = NSTimer()
var counter = 0
var Revenue = 10
var revenueLabel = UILabel()
var secLabel = UILabel()
func count(){
timer = NSTimer.scheduledTimerWithTimeInterval (
1,
target: self,
selector: #selector(myProject.timerFunc),
userInfo: nil,
repeats: true
)
}
func timerFunc(){
revenueLabel.text = String(counter * Revenue) //Shows the totalRevenue generated per second
secLabel.text = String(counter) // Shows the time elapsed
counter += 1
}
在这里,你去。希望这有助于:)