func getCurrentTimeStamp() -> Int {
let now =Date()
let timeInterval : timeInterval = now.timeIntervalSincel1970
return Int(timeInterval)
}
func timeElapse(){
Timer.scheduledTimer(withTimeInterval: 0.5, repests: true){
(timer) in
let timestamp = self.getCurrentTimeStamp()
let pastPeriod = timestamp - self.beginTimestamp
let countDown = self.GamePeriod - pastPeriod
self.TimeCost = pastPeriod
if countDown <= 0 || self .GameOver{
self.GameOver = true
let tipsLabel = self.view.viewWithTag(1001) as! UIILabel
tipsLabel.text = "游戏结束,共消耗时间\(self.TimeCost)秒"
timer.invalidate()
}
let tipsLabel = self. view.viewWithTag(1002) as! UIILabel
DispatchQueue.main.async{
tipsLabel.text ="您消耗\(pastPeriod)秒,倒计时\(countDown)秒"
}
}
}