Date Picker-Ca$h十年回顧
~歡迎來到Ca$h的十年回顧~
十年之前,我不認識Swift,Swift不屬於我,
我還是背著吉他,彈奏一個C和弦刷Chord,
走過台北、雪梨的街頭。
十年之後,彼得潘出沒,
教給我頻果,只是Java不懂,
再也找不回年少時的夢,
夢想最後終究收藏App中。
使用Button、Slider、DatePicker,
控制這十年來我的圖文陣列。
拉好UI元件、宣告變數index、照片名稱陣列、顯示字串陣列
Button按鈕控制下一年的圖文
code:
@IBAction func ClickChangeBt(_ sender: Any) {
index += 1
if index == names.count{
index = 0
}
MyImage.image = UIImage(named: names[index])
if index == 10 {
let index = 0
NumLabel.text = “202\(index)”
} else {
NumLabel.text = “201\(index)”
}
textView.text = textStrings[index]
ImageSD.value = Float(index)
let components = DateComponents(calendar: Calendar.current,year: 2010 + index)
datePicker.date = components.date!
}
Slider拉桿控制下一年的圖文
code:
@IBAction func ImageChangeSD(_ sender: UISlider) {
let index = Int(sender.value.rounded())
if index == 10 {
let index = 0
NumLabel.text = “202\(index)”
} else {
NumLabel.text = “201\(index)”
}
textView.text = textStrings[index]
MyImage.image = UIImage(named: names[index])
let components = DateComponents(calendar: Calendar.current,year: 2010 + index)
datePicker.date = components.date!
}
DatePicker控制下一年的圖文
code:
@IBAction func ChangeDate(_ sender: UIDatePicker) {
let year = Calendar.current.component(.year, from: sender.date)
let index = year — 2010
MyImage.image = UIImage(named: names[index])
if index == 10 {
let index = 0
NumLabel.text = “202\(index)”
} else {
NumLabel.text = “201\(index)”
}
textView.text = textStrings[index]
ImageSD.value = Float(index)
}
GitHub連結: