参考网页:http://segmentfault.com/a/1190000002420095
写的真不错。赞
横向滑动的手势操作则是这两行代码:
let panGestureRecognizer = UIPanGestureRecognizer(target: self,action: "handlePanGesture:")
centerNavigationController.view.addGestureRecognizer(panGestureRecognizer)
handlePanGesture
处理了滑动手势:
func handlePanGesture(recognizer: UIPanGestureRecognizer) {
// 判断是否从左向右
let gestureIsDraggingFromLeftToRight = (recognizer.velocityInView(view).x > 0)
switch(recognizer.state) {
// 刚刚开始滑动
case .Began:
// 如果刚刚开始滑动的时候还处于主页面,则根据左右方向加入侧面菜单
if (currentState == .BothCollapsed) {
if (gestureIsDraggingFromLeftToRight) {
addLeftPanelViewController()
} else {
addRightPanelViewController()
}
}
// 如果是正在滑动,则偏移主视图的坐标实现跟随手指位置移动
case .Changed:
recognizer.view!.center.x = recognizer.view!.center.x + recognizer.translationInView(view).x
recognizer.setTranslation(CGPointZero,inView: view)
// 如果滑动结束
case .Ended:
// 判断该左还是该右
if (leftViewController != nil) {
let hasMovedGreaterThanHalfway = recognizer.view!.center.x > view.bounds.size.width animateLeftPanel(shouldExpand: hasMovedGreaterThanHalfway) } else if (rightViewController != nil) {
let hasMovedGreaterThanHalfway = recognizer.view!.center.x < 0
animateRightPanel(shouldExpand: hasMovedGreaterThanHalfway)
}
default:
break
}
}
原文链接:https://www.f2er.com/swift/324948.html