1 继承 UITabBarController (这个例子是这样)
2 Setup gestures (L and R)
override func viewDidLoad() { super.viewDidLoad() /* setup switch gestures */ self.swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: "onSwipeGesture:") self.swipeLeftGesture!.direction = UISwipeGestureRecognizerDirection.Left self.view.addGestureRecognizer(self.swipeLeftGesture!) self.swipeRightGesture = UISwipeGestureRecognizer(target: self, action: "onSwipeGesture:") self.swipeRightGesture!.direction = UISwipeGestureRecognizerDirection.Right self.view.addGestureRecognizer(self.swipeRightGesture!)}
3 实现gestures callback
/* gestures callback */@IBAction private func onSwipeGesture (sender: UISwipeGestureRecognizer) { print(__LINE__, __FUNCTION__) if (nil == self.viewControllers) { print(__LINE__, "nil viewControllers") return } let dire = sender.direction var l = false var r = false switch (dire) { case UISwipeGestureRecognizerDirection.Left: /* right-to-left => left: need to show the right one */ print(__LINE__, "UISwipeGestureRecognizerDirection.Left") l = true case UISwipeGestureRecognizerDirection.Right: print(__LINE__, "UISwipeGestureRecognizerDirection.Right") r = true default: print(__LINE__, "UISwipeGestureRecognizerDirection.xx unhandled") } let si = self.selectedIndex let c = self.viewControllers!.count print(__LINE__, "si: \(si) c: \(c)") var fromView: UIView? var toShow: UIView? if (l && (si < (c - 1))) { /* right-to-left => left: need to show the right one */ fromView = self.selectedViewController!.view toShow = self.viewControllers![si + 1].view } else if (r && (si > 0)) { fromView = self.selectedViewController!.view toShow = self.viewControllers![si - 1].view } if (nil != toShow) { if (l) { /* right-to-left => left: need to show the right one */ UIView.transitionFromView(fromView!, toView: toShow!, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromRight, completion: { if ($0 && (nil != self.viewControllers)) { let si = self.selectedIndex let to = si + 1 self.selectedViewController = self.viewControllers![to] } }) } else { UIView.transitionFromView(fromView!, toView: toShow!, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromLeft, completion: { if ($0 && (nil != self.viewControllers)) { let si = self.selectedIndex let to = si - 1 self.selectedViewController = self.viewControllers![to] } }) } }}var swipeLeftGesture: UISwipeGestureRecognizer?var swipeRightGesture: UISwipeGestureRecognizer?