筆記風(fēng)格借鑒Knight_SJ
需求點(diǎn):
通過觸發(fā)鍵盤實(shí)現(xiàn)更新視圖約束的動(dòng)畫效果,網(wǎng)上有很多通過Masonry實(shí)現(xiàn)動(dòng)畫更新約束的教程西疤,本人用Swift簡單實(shí)現(xiàn)了下。
效果如下:
特點(diǎn):
獲取鍵盤彈出和回收時(shí)間,實(shí)時(shí)更新約束拉宗,實(shí)現(xiàn)彈出或回收鍵盤與更新約束動(dòng)畫時(shí)間同步。
更新約束一共分為三個(gè)步驟:
- 創(chuàng)建鍵盤彈出和隱藏系統(tǒng)通知
- 實(shí)現(xiàn)通知方法
- 通過調(diào)用needsUpdateConstraints及updateConstraintsIfNeeded實(shí)現(xiàn)動(dòng)畫更新
示例Demo:
觸發(fā)彈出或回收鍵盤通知方法中改變約束
1、首先創(chuàng)建鍵盤彈出和隱藏系統(tǒng)通知
// 鍵盤將要彈出
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
// 鍵盤將要回彈
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
2旦事、實(shí)現(xiàn)通知方法
// MARK: - 鍵盤將要彈出
func keyboardWillShow(notification: Notification) {
let userInfo = notification.userInfo
let aValue = userInfo?[UIKeyboardAnimationDurationUserInfoKey]
SLLog("彈出\(aValue!)")
// 判斷是否是第一次觸發(fā)開始編輯魁巩,如果是第一次則改變約束
if isUpdataExpand == false {
updateWithExpand(isExpanded: true, animated: true, duration: aValue! as! TimeInterval)
isUpdataExpand = true
}
}
// MARK: - 鍵盤將要回收
func keyboardWillHide(notification: Notification) {
let userInfo = notification.userInfo
let aValue = userInfo?[UIKeyboardAnimationDurationUserInfoKey]
SLLog("回收\(aValue!)")
updateWithExpand(isExpanded: false, animated: true, duration: aValue! as! TimeInterval)
// 回收把isUpdataExpand設(shè)置為false
isUpdataExpand = false
}
3.更新約束
// MARK: - 更新約束
func updateWithExpand(isExpanded:Bool, animated:Bool, duration:TimeInterval = 0.25) {
// 重要:更新約束需要調(diào)用updateConstraints閉包
soolifeImg.snp.updateConstraints { (make) in
if isExpanded == false{
make.centerX.equalTo(self)
make.width.equalTo(SCREEN_W/4)
make.top.equalTo(self.snp.top).offset(AD_HEIGHT(90))
make.height.equalTo(AD_HEIGHT(120))
}else{
make.centerX.equalTo(self)
make.width.equalTo(SCREEN_W/5)
make.top.equalTo(self.snp.top).offset(AD_HEIGHT(50))
make.height.equalTo(AD_HEIGHT(80))
}
}
accountIT.snp.updateConstraints { (make) in
make.left.equalTo(self.snp.left).offset(AD_WIDTH(15))
make.right.equalTo(self.snp.right).offset(AD_WIDTH(-15))
make.height.equalTo(AD_HEIGHT(45))
if isExpanded == false{
make.top.equalTo(soolifeImg.snp.bottom).offset(AD_HEIGHT(60))
}else{
make.top.equalTo(soolifeImg.snp.bottom).offset(AD_HEIGHT(40))
}
}
passwordIT.snp.updateConstraints { (make) in
make.left.equalTo(self.snp.left).offset(AD_WIDTH(15))
make.right.equalTo(self.snp.right).offset(AD_WIDTH(-15))
make.height.equalTo(AD_HEIGHT(45))
make.top.equalTo(accountIT.snp.bottom).offset(AD_HEIGHT(5))
}
loginBtn.snp.updateConstraints { (make) in
make.top.equalTo(passwordIT.snp.bottom).offset(AD_HEIGHT(40))
make.left.equalTo(self.snp.left).offset(AD_WIDTH(20))
make.right.equalTo(self.snp.right).offset(AD_WIDTH(-20))
make.height.equalTo(AD_HEIGHT(40))
}
if animated == true {
// 告訴self.view約束需要更新
self.needsUpdateConstraints()
// 調(diào)用此方法告訴self.view檢測是否需要更新約束,若需要?jiǎng)t更新姐浮,下面添加動(dòng)畫效果才起作用
self.updateConstraintsIfNeeded()
// 更新動(dòng)畫
UIView.animate(withDuration: duration, animations: {
self.layoutIfNeeded()
})
}
}