DatePicker練習 計算生命靈數

兩光師來算命囉

來幫自己算算命吧!!

SeaFood不好意思,借我練習一下><

用到的功能:

  1. UIDatePicker
  2. UIButton

能觸發的IBAction有

  1. 嗯…就只有UIButton,按下Button後,會抓取選取的日期,

再來是計算出年紀.星座.生肖及生命密碼

而我的習慣是把資訊都用陣列包起來,包含星座.生肖及狀態

//預設陣列
var newConstellation = ["水瓶座","雙魚座","白羊座","金牛座","雙子座","巨蟹座","獅子座","處女座","天秤座","天蠍座","射手座","摩羯座"]
var newAnimal = ["鼠","牛","虎","兔","龍","蛇","馬","羊","猴","雞","狗","豬"]
var passwdStatusMessage = ["","財運","事業","愛情","親情","友情","運氣","健康","眼睛","學業","天氣"]

處理星座:基本上判斷日期後,抓取陣列內的第N筆

//處理星座
let constellationNumber: Int
if month == 1 && day >= 19 || month == 2 && day <= 19 {
//水瓶座
constellationNumber = 0
}else if month == 2 && day >= 20 || month == 3 && day <= 20 {
//雙魚座
constellationNumber = 1
}else if month == 3 && day >= 21 || month == 4 && day <= 20 {
//白羊座
constellationNumber = 2
}else if month == 4 && day >= 21 || month == 5 && day <= 20 {
//金牛座
constellationNumber = 3
}else if month == 5 && day >= 21 || month == 6 && day <= 20 {
//雙子座
constellationNumber = 4
}else if month == 6 && day >= 21 || month == 7 && day <= 22 {
//巨蟹座
constellationNumber = 5
}else if month == 7 && day >= 23 || month == 8 && day <= 22 {
//獅子座
constellationNumber = 6
}else if month == 8 && day >= 23 || month == 9 && day <= 22 {
//處女座
constellationNumber = 7
}else if month == 9 && day >= 23 || month == 10 && day <= 22 {
//天秤座
constellationNumber = 8
}else if month == 10 && day >= 23 || month == 11 && day <= 22 {
//天蠍座
constellationNumber = 9
}else if month == 11 && day >= 23 || month == 12 && day <= 22 {
//射手座
constellationNumber = 10
}else{
//摩羯座
constellationNumber = 11
}

處理年紀:抓取選擇的欄位及現在時間,去做加減動作

let today = Date()
let nowComponents = Calendar.current.dateComponents(in: TimeZone.current, from: today)
let nowYear = nowComponents.year!
var age = nowYear - year //年紀
//年紀小於0,顯示0
if age < 0 {
age = 0
}

處理生肖:(選擇的年份-4 )% 12 的值 就是陣列內的第n筆

//處理生肖
let animalNumber = (year - 4) % 12 // (選的年份-4) % 12 取餘數 = 陣列第幾筆
//更換資訊
changeFile(constellationNumber: constellationNumber, animalNumber: animalNumber,age: age)
var passwd : Int = getPasswd()
passwdLabel.text = "\(passwd)"
passwdStatusLabel.text = passwdStatusMessage[passwd]

Demo部分

附上我的Github

--

--