我想要根据时间显示UIImage
。{例如,在上午9点到下午5点之间,我想显示一个“打开”的图像,其余时间“关闭”。如何使用SWIFT实现此目的。
目标:
答案 0 :(得分:2)
使用这些代码行
我希望你能得到的东西,你正在寻找
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour, fromDate: date)
let hour = components.hour
if(hour >= 9 && hour <= 17)
{
//set your image 'open image'
println("Open")
}
else{
//set your image 'close image'
println("closed")
}
答案 1 :(得分:1)
适用于Swift 2
在课程下面放置以下内容:
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
将其放在viewDidLoad中:
let components = calendar.components(.Hour, fromDate: date)
let hour = components.hour
if(hour >= 9 && hour <= 17) {
// set your image 'open image'
print("Open")
}
else {
// set your image 'close image'
print("Closed")
}