终于有时间继续写我的文章了,这段时间在赶学校的软件课程设计,可算弄完了!
下面继续我们的创造之旅~
本篇文章你会学到
- 用KVO方法优化键盘弹出动画
- 将同步下载消息改为异步,以减轻主线程的压力。
- 实现app登录、注册的功能
首先下载本章源代码:
百度网盘地址
在上一章结尾我提到:
我们的app在键盘弹出时有一些问题: - 在我们点出键盘时会遮挡消息:
iOS Simulator Screen Shot 2015年9月8日 下午4.14.55.png
- 键盘弹出时把tableView拉到底部会有一个很难看的空白:
iOS Simulator Screen Shot 2015年9月8日 下午4.15.21.png
下面我们来解决它,我们需要在键盘弹出时修改tableView的一些属性和约束条件,所以我们需要在键盘弹出时得到通知,要做到这个,我们要使用KVO(Key-Value Observing)方法。
在viewDidLoad()中的结尾添加以下代码来添加键值监控:123let notificationCenter = NSNotificationCenter.defaultCenter()notificationCenter.addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)notificationCenter.addObserver(self, selector: "keyboardDidShow:", name: UIKeyboardDidShowNotification, object: nil)首先获取通知中心的实例,然后添加两个观察者,第一个用来监控
UIKeyboardWillShowNotification
键值的变化,这是系统提供的键值,当键盘将要弹出时会改变;第二个监控UIKeyboardDidShowNotification
,同样地,这也是系统提供的,当键盘完全弹出时会改变。
当这两个键值改变时,会向通知中心发送通知,然后由我们自定义的两个selector
方法处理通知,下面定义这两个方法。
首先第一个方法:12345678910111213141516171819202122func keyboardWillShow(notification: NSNotification) {let userInfo = notification.userInfo as NSDictionary!let frameNew = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()let insetNewBottom = tableView.convertRect(frameNew, fromView: nil).heightlet insetOld = tableView.contentInsetlet insetChange = insetNewBottom - insetOld.bottomlet overflow = tableView.contentSize.height - (tableView.frame.height-insetOld.top-insetOld.bottom)let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValuelet animations: (() -> Void) = {if !(self.tableView.tracking || self.tableView.decelerating) {// 根据键盘位置调整Insetif overflow > 0 {self.tableView.contentOffset.y += insetChangeif self.tableView.contentOffset.y -overflow {self.tableView.contentOffset.y += insetChange + overflow}}}if duration > 0 {let options = UIViewAnimationOptions(UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).integerValue很难懂?不着急,我们一步一步解释这些代码!
首先取出通知的userifno,键盘的所有属性都在这里面,他是一个字典类型的数据:1let userInfo = notification.userInfo as NSDictionary!然后通过
UIKeyboardFrameEndUserInfoKey
key取出键盘的位置、大小信息,也就是frame,并将其的参考view设置为tableView,记录下它的高度12let frameNew = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()let insetNewBottom = tableView.convertRect(frameNew, fromView: nil).height然后我们需要计算一些数据:
123let insetOld = tableView.contentInsetݠ会学到- 用KVO方法优化键盘弹出动画
- 将同步下载消息改为异步,以减轻主线程的压力。
- 实现app登录、注册的功能
首先下载本章源代码:
百度网盘地址
在上一章结尾我提到:
我们的app在键盘弹出时有一些问题: - 在我们点出键盘时会遮挡消息:
iOS Simulator Screen Shot 2015年9月8日 下午4.14.55.png
- 键盘弹出时把tableView拉到底部会有一个很难看的空白:
iOS Simulator Screen Shot 2015年9月8日 下午4.15.21.png
下面我们来解决它,我们需要在键盘弹出时修改tableView的一些属性和约束条件,所以我们需要在键盘弹出时得到通知,要做到这个,我们要使用KVO(Key-Value Observing)方法。
在viewDidLoad()中的结尾添加以下代码来添加键值监控:123let notificationCenter = NSNotificationCenter.defaultCenter()notificationCenter.addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)notificationCenter.addObserver(self, selector: "keyboardDidShow:", name: UIKeyboardDidShowNotification, object: nil)首先获取通知中心的实例,然后添加两个观察者,第一个用来监控
UIKeyboardWillShowNotification
键值的变化,这是系统提供的键值,当键盘将要弹出时会改变;第二个监控UIKeyboardDidShowNotification
,同样地,这也是系统提供的,当键盘完全弹出时会改变。
当这两个键值改变时,会向通知中心发送通知,然后由我们自定义的两个selector
方法处理通知,下面定义这两个方法。
首先第一个方法:12345678910111213141516171819202122func keyboardWillShow(notification: NSNotification) {let userInfo = notification.userInfo as NSDictionary!let frameNew = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()let insetNewBottom = tableView.convertRect(frameNew, fromView: nil).heightlet insetOld = tableView.contentInsetlet insetChange = insetNewBottom - insetOld.bottomlet overflow = tableView.contentSize.height - (tableView.frame.height-insetOld.top-insetOld.bottom)let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValuelet animations: (() -> Void) = {if !(self.tableView.tracking || self.tableView.decelerating) {// 根据键盘位置调整Insetif overflow > 0 {self.tableView.contentOffset.y += insetChangeif self.tableView.contentOffset.y -overflow {self.tableView.contentOffset.y += insetChange + overflow}}}if duration > 0 {let options = UIViewAnimationOptions(UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).integerValue很难懂?不着急,我们一步一步解释这些代码!
首先取出通知的userifno,键盘的所有属性都在这里面,他是一个字典类型的数据:1let userInfo = notification.userInfo as NSDictionary!然后通过
UIKeyboardFrameEndUserInfoKey
key取出键盘的位置、大小信息,也就是frame,并将其的参考view设置为tableView,记录下它的高度12let frameNew = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()let insetNewBottom = tableView.convertRect(frameNew, fromView: nil).height然后我们需要计算一些数据:
123let insetOld = tableView.contentInsetm - insetOld.bottom let overflow = tableView.contentSize.height - (tableView.frame.height-insetOld.top-insetOld.bottom) let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue let animations: (() -> Void) = { if !(self.tableView.tracking || self.tableView.decelerating) { // 根据键盘位置调整Inset if overflow > 0 { self.tableView.contentOffset.y += insetChange if self.tableView.contentOffset.y -overflow { self.tableView.contentOffset.y += insetChange + overflow } } } if duration