Swift学习记录 Day 2

Day 2 


Swift学习记录 Day 2_第1张图片

根据 100 Days of Swift 第三篇内容和知识点, 开发学习

覆盖知识面:

1.Write an app in MVC pattern

首先搭建MVC架构,可根据自己实际经验选择MVVM 或者MVP 构建,在OC中我习惯使用MVVM, 但是MVVC通过ReactiveCocoa来构建最好,Swift中我还不会使用RXSwift和ReactiveSwift,所以就根据知识点一步一步走。

2.Work with Computed Properties

3.Create a toolbar above the keyboard

为keyboard创建一个toolbar,实际实现思路为创建一个View,在View里监听keyboardshow和keyboardhidden的通知,从userinfo中获取偏移量和动画时间, 再对toolbar 进行移动, 达到keyboard.toolbar的功能。

Done 按钮实现回调,通过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 加入按键事件,并调用callBack

 @objcfuncbtnClick(_sender:UIButton){

        callBack!()

    }

ViewController中

toolbar 不要使用懒加载, 因为懒加载对导致闭包中的self无法使用,同时需要注意 闭包循环引用的问题

var  keyboardView:KeyBoardStatusView?

keyboardView = KeyBoardStatusView.init({

            weakvarweakSelf =self

            weakSelf?.NumberTextField.resignFirstResponder()

        })


4.Set min and max values for Slider

使用storyboard拖拽,可以直接在interface build上设置

5.Convert Strings to Int / Double and vice versa

Double() 来进行转换

6.Handle calculations with formulas written in the Model

使用MVC 架构 就是为了让ViewController能够瘦身,那么一些数据转换,计算的逻辑都需要放在Model中,我的做法是将Slider.value 赋值给Model中的一个属性, 给这个属性设置观察者,在改变的时候同时改变其他属性的值

7.Enable or disable objects based on if the keyboard is open

textField的delegate方法中进行设置

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)

每当Slider的value变化时,赋值给到model中value

你可能感兴趣的:(Swift学习记录 Day 2)