Ios Xcode中的时间利用

Ios Xcode中的时间利用,ios,swift2,xcode7,Ios,Swift2,Xcode7,我想在我的应用程序中使用“时间”。我有一个“每秒收入”的值,然后我想要一个标签来告诉我我总共取得了多少收入。 所以它应该是这样的:收入/秒*经过的秒数=总收入。我希望号码一直在变。 怎么做?您需要查找NSTimer类,它有一个静态方法来创建一个无限重复的计时器 将其设置为每秒重复一次,并使用新值更新标签,就可以开始了:) 现在无法从我的手机链接,但任何人都可以使用指向文档的链接编辑我的答案 var timer = NSTimer() var counter = 0 var Revenue = 1

我想在我的应用程序中使用“时间”。我有一个“每秒收入”的值,然后我想要一个标签来告诉我我总共取得了多少收入。 所以它应该是这样的:收入/秒*经过的秒数=总收入。我希望号码一直在变。
怎么做?

您需要查找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
}
给你。希望这有帮助:)