Day 2?
根據(jù)?100 Days of Swift?第三篇內(nèi)容和知識點侮攀, 開發(fā)學(xué)習(xí)
覆蓋知識面:
1.Write an app in MVC pattern
首先搭建MVC架構(gòu)宏侍,可根據(jù)自己實際經(jīng)驗選擇MVVM 或者MVP 構(gòu)建塘匣,在OC中我習(xí)慣使用MVVM, 但是MVVC通過ReactiveCocoa來構(gòu)建最好,Swift中我還不會使用RXSwift和ReactiveSwift豁陆,所以就根據(jù)知識點一步一步走篇裁。
2.Work with Computed Properties
3.Create a toolbar above the keyboard
為keyboard創(chuàng)建一個toolbar沛慢,實際實現(xiàn)思路為創(chuàng)建一個View,在View里監(jiān)聽keyboardshow和keyboardhidden的通知达布,從userinfo中獲取偏移量和動畫時間团甲, 再對toolbar 進行移動, 達到keyboard.toolbar的功能黍聂。
Done 按鈕實現(xiàn)回調(diào)躺苦,通過toolbar 初始化時傳入一個閉包
Toolbar中
typealiasclickBlock = ()->Void
? ? var callBack:clickBlock?
? ? convenience init(_ block: @escaping () -> Void){
? ? ? ? self.init(frame:CGRect.init(x:0, y:UIScreen.main.bounds.height-40, width:UIScreen.main.bounds.width, height:40))
? ? ? ? setup()
? ? ? ? callBack= block
? ? }
為Btn 加入按鍵事件,并調(diào)用callBack
?@objcfuncbtnClick(_sender:UIButton){
? ? ? ? callBack!()
? ? }
ViewController中
toolbar 不要使用懶加載产还, 因為懶加載對導(dǎo)致閉包中的self無法使用匹厘,同時需要注意 閉包循環(huán)引用的問題
var? keyboardView:KeyBoardStatusView?
keyboardView = KeyBoardStatusView.init({
? ? ? ? ? ? weakvarweakSelf =self
? ? ? ? ? ? weakSelf?.NumberTextField.resignFirstResponder()
? ? ? ? })
4.Set min and max values for Slider
使用storyboard拖拽,可以直接在interface build上設(shè)置
5.Convert Strings to Int / Double and vice versa
Double() 來進行轉(zhuǎn)換
6.Handle calculations with formulas written in the Model
使用MVC 架構(gòu) 就是為了讓ViewController能夠瘦身脐区,那么一些數(shù)據(jù)轉(zhuǎn)換愈诚,計算的邏輯都需要放在Model中,我的做法是將Slider.value 賦值給Model中的一個屬性坡椒, 給這個屬性設(shè)置觀察者扰路,在改變的時候同時改變其他屬性的值
7.Enable or disable objects based on if the keyboard is open
textField的delegate方法中進行設(shè)置
8.Get values from the UISlider and convert them to Int for use in the Model
給UISider.value 添加一個target
CountSlider.addTarget(self, action:#selector(sliderValueChange(slider:)), for:UIControlEvents.valueChanged)
每當(dāng)Slider的value變化時尤溜,賦值給到model中value