UI 元件利用Date Picker,TextField,自定義鍵盤 inputView 實作<我的人生倒數 Mylife countdown>

製作動機

成功學大師史蒂芬‧柯維(Stephen Covey)強調,唯有先確認人生目標,才能引領自己走往正確方向,這種「我設計我的人生」的做法,正是柯維所提出成功者的第二個習慣: 以終為始(begin with the end in mind)

最近看了 史蒂芬·柯維(Stephen R.Covey)在《高效人士的七個習慣(The 7 habits of Highly Effective People)裡面以終為始的概念蠻有意思的

以終為始

因為以終為始所以心無旁騖,確立好目標就確實執行

那麼在確立好目標前審視自己還剩下多少資源(時間) 是必須的

於是製作出這款 <我的人生倒數 Mylife countdown>去檢視自己剩下的時間

也提醒自己不要虛度時間

實作 Date Picker , Textfiled

Date Picker與自定義鍵盤自定義鍵盤 inputView和 inputAccessoryView

目標:將鍵盤替換成Date Picker方便我們選取出生年月日

程式碼先附上:

於是這次用的是 介紹 inputView和 InputAccessoryView將InputAccessoryView 替換成我們建立的toolbar 與 inputView 換成dataPicker並且在toolbar上新增一個叫doneBtn的BarButton

#inputView
inputView就是顯示鍵盤的視圖,
如果重置這個視圖則不再退出鍵盤,或者替換自己的視圖。
如果想實現當某些關鍵控件變為第一響應者時不添加鍵盤或重新插入我們自定義的界面,
那麼我們就可以通過修改這個inputView來實現,替換放置一個日期替換器,或者自定義表情鍵盤。
#特性
inputView將不會鍵盤出現而出現,
設置了InputView僅當UITextField或UITextView變為第一相應者時顯示出來,不會顯示鍵盤了。
#InputAccessoryView
設置了InputAccessoryView,
它會通過鍵盤一起出現並且會顯示在鍵盤的頂端。
#inputAccessoryView默認為零

關於UIDtatPicker 取的時間日期方法(in Swift5)

// Get date picker data
let components = datePicker.calendar.dateComponents([.day, .month, .year],
from: datePicker.date)
let day = components.day
let month = components.month
let year = components.year

計算邏輯

目標:計算人生剩餘時間並顯示其天數

先計算出現使用者年齡再扣除國人平均壽命約80歲=剩餘時間

func dateInterval(startTime:String,endTime:String) -> Int{let dateFormatter = DateFormatter()dateFormatter.dateFormat = "yyyy/MM/dd"guard let date1 = dateFormatter.date(from: startTime),let date2 = dateFormatter.date(from: endTime) else {return -1}let components = NSCalendar.current.dateComponents([.day], from: date1, to: date2)//如果需要傳回月份,分鐘等等,只需要在dateComponents参数後面加上對應的参數即可,示例如下:
// let components = NSCalendar.current.dateComponents([.month,.day,.hour,.minute], from: date1, to: date2)
return components.day!}

這邊關於日期處理附上我所參考的資料

計算時間差 資料 為CSDN博主「星光uoin」的文章

然後將計算出的數字傳到第二個page就大功告成

GitHub網址:

後記

將自己剩餘生命顯示在一塊沒有生命的電子螢幕上,也許感覺很殘忍。

但不管感受如何,時間只會繼續的前進。

能做的只有

把握當下,充實自己

最後附上個人很喜歡的 WWDC20 Event Keynote opening music

AURORA — Daydreamer

在有限的時間內 實踐自己的Dream

Don’t just dream about your life, live your dreams.

-我的書桌-

I don’t want you to chase your dream , I want you to chase Reality

— Christopher Nolan 克里斯多福·諾蘭

希望這篇文章能夠幫助到你(妳)

如有錯誤指正

I hope you found this guide helpful. If not, then please let me know either in the comments below, I’m AlberLee

Swift#4

--

--