公司項(xiàng)目一直沒有推進(jìn), 只能自己寫一些Swift代碼, 準(zhǔn)備全面轉(zhuǎn)向Swift開發(fā).
之前的項(xiàng)目中使用過抽屜效果, 一直都是使用第三方, 準(zhǔn)備自己一個(gè)抽屜
最終效果
運(yùn)行效果
1. Storyboard
- 本人懶, 就沒有搭UI, 使用兩張截圖
-
在 HomeViewController 中添加 UIPanGestureRecognizer, 并進(jìn)行關(guān)聯(lián)
屏幕快照 2016-05-18 16.12.41.png
2. MeunViewController
- 通過 Storyboard 的 取出 HomeViewController 的 view 添加到subview
- 給 HomeViewController 的 UIPanGestureRecognizer 添加響應(yīng)事件
// 通過 Storyboard 取出 HomeViewController 的 view, 放在背景上
homeViewController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
self.view.addSubview(homeViewController.view)
// 綁定 UIPanGestureRecognizer
homeViewController.panGusture.addTarget(self, action: #selector(pan))
3.響應(yīng)事件
struct Common {
static let screenWidth = UIScreen.mainScreen().bounds.size.width
static let screenHeight = UIScreen.mainScreen().bounds.size.height
}
var homeViewController: HomeViewController!
var distance: CGFloat = 0
let Proportion: CGFloat = 0.3
// 響應(yīng) UIPanGestureRecognizer 事件
@objc private func pan(recongnizer: UIPanGestureRecognizer) {
// 獲取實(shí)時(shí)距離
let x = recongnizer.translationInView(self.view).x
let trueDistance = distance + x
// 如果 UIPanGestureRecognizer 結(jié)束, 則自動(dòng)停靠
if recongnizer.state == UIGestureRecognizerState.Ended {
print(x)
// 判斷停靠位置
if trueDistance > Common.screenWidth * Proportion {
showLeft()
} else {
showHome()
}
return
}
// 只添加左抽屜
if trueDistance < 0 {
return
}
recongnizer.view!.center = CGPointMake(self.view.center.x + trueDistance, self.view.center.y)
}
4 . 手勢(shì)響應(yīng)事件中方法
private func showLeft() {
distance = self.view.center.x * 1.5
doAnmation()
}
private func showHome() {
distance = 0
doAnmation()
}
private func doAnmation() {
UIView.animateWithDuration(0.3,
delay: 0,
options: .CurveEaseInOut,
animations: { () -> Void in
self.homeViewController.view.center = CGPointMake(self.view.center.x + self.distance, self.view.center.y)
},
completion: nil)
}
}
結(jié)語
只是實(shí)現(xiàn)了一個(gè)側(cè)滑的效果, 并沒有實(shí)現(xiàn)相關(guān)的功能, 待完善