在Swift中创建倒数计时器

时间:2016-11-10 14:28:35

标签: ios swift xcode timer countdown

我写了这段代码,倒计时到特定日期。

我如何制作它以使倒计时的每个元素都显示在单独的标签中。

所以我会有一个标签显示天数,标签显示小时数,标签显示分钟数,标签显示秒数。

代码:

class ViewController: UIViewController {

@IBOutlet weak var dateLabelOutlet: UILabel!

let currentDate = Date()
let dateFormatter = DateFormatter()

let userCalendar = Calendar.current

let requestedComponent: Set<Calendar.Component> = [.day,.hour,.minute,.second]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(printTime), userInfo: nil, repeats: true)
    timer.fire()

}

func printTime()
{
    dateFormatter.dateFormat = "dd/MM/yy hh:mm:ss"
    let startTime = Date()
    let endTime = dateFormatter.date(from: "25/12/16 00:00:00")
    let timeDifference = userCalendar.dateComponents(requestedComponent, from: startTime, to: endTime!)

    dateLabelOutlet.text = "\(timeDifference.day!) Days \(timeDifference.hour!) Hours \(timeDifference.minute!) Minutes \(timeDifference.second!) Seconds"
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

1 个答案:

答案 0 :(得分:0)

您的代码已经计算了dateLabelOutlet.text = "\(timeDifference.day!) Days \(timeDifference.hour!) Hours \(timeDifference.minute!) Minutes \(timeDifference.second!) Seconds" 中的差异,然后构建了一个字符串,其中包含日,小时,分钟和秒差异。

只需更改此行:

timeDifference.day

您需要创建4个标签并将timeDifference.hour放在一个标签中,timeDifference.minute放在另一个标签中,timeDifference.second放在第三个标签中,import Foundation class CSVScanner { class func debug(string:String){ println("CSVScanner: \(string)") } class func runFunctionOnRowsFromFile(theColumnNames:Array<String>, withFileName theFileName:String, withFunction theFunction:(Dictionary<String, String>)->()) { if let strBundle = NSBundle.mainBundle().pathForResource(theFileName, ofType: "csv") { var encodingError:NSError? = nil if let fileObject = NSString(contentsOfFile: strBundle, encoding: NSUTF8StringEncoding, error: &encodingError){ var fileObjectCleaned = fileObject.stringByReplacingOccurrencesOfString("\r", withString: "\n") fileObjectCleaned = fileObjectCleaned.stringByReplacingOccurrencesOfString("\n\n", withString: "\n") let objectArray = fileObjectCleaned.componentsSeparatedByString("\n") for anObjectRow in objectArray { let objectColumns = anObjectRow.componentsSeparatedByString(",") var aDictionaryEntry = Dictionary<String, String>() var columnIndex = 0 for anObjectColumn in objectColumns { aDictionaryEntry[theColumnNames[columnIndex]] = anObjectColumn.stringByReplacingOccurrencesOfString("\"", withString: "", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil) columnIndex++ } if aDictionaryEntry.count>1{ theFunction(aDictionaryEntry) }else{ CSVScanner.debug("No data extracted from row: \(anObjectRow) -> \(objectColumns)") } } }else{ CSVScanner.debug("Unable to load csv file from path: \(strBundle)") if let errorString = encodingError?.description { CSVScanner.debug("Received encoding error: \(errorString)") } } }else{ CSVScanner.debug("Unable to get path to csv file: \(theFileName).csv") } } } 放在第四个标签中

如果您编写了该代码,我认为这很明显 - 或者是您复制/粘贴的其他人的代码?