-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLxTabBarController.swift
221 lines (167 loc) · 7.59 KB
/
LxTabBarController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// LxTabBarController.swift
// LxTabBarControllerDemo
//
import UIKit
enum LxTabBarControllerInteractionStopReason {
case Finished, Cancelled, Failed
}
let LxTabBarControllerDidSelectViewControllerNotification = "LxTabBarControllerDidSelectViewControllerNotification"
private enum LxTabBarControllerSwitchType {
case Unknown, Last, Next
}
let TRANSITION_DURATION = 0.2
private var _switchType = LxTabBarControllerSwitchType.Unknown
class Transition: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
return TRANSITION_DURATION
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
transitionContext.containerView().insertSubview(toViewController.view, aboveSubview: fromViewController.view)
switch _switchType {
case .Last:
toViewController.view.frame.origin.x = -toViewController.view.frame.size.width
case .Next:
toViewController.view.frame.origin.x = toViewController.view.frame.size.width
case .Unknown:
break
}
UIView.animateWithDuration(TRANSITION_DURATION, animations: { () -> Void in
switch _switchType {
case .Last:
fromViewController.view.frame.origin.x = fromViewController.view.frame.size.width
case .Next:
fromViewController.view.frame.origin.x = -fromViewController.view.frame.size.width
case .Unknown:
break
}
toViewController.view.frame = transitionContext.containerView().bounds
}) { (finished) -> Void in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
}
}
}
class LxTabBarController: UITabBarController,UITabBarControllerDelegate,UIGestureRecognizerDelegate {
var panToSwitchGestureRecognizerEnabled: Bool {
get {
return _panToSwitchGestureRecognizer.enabled
}
set {
_panToSwitchGestureRecognizer.enabled = newValue
}
}
var recognizeOtherGestureSimultaneously = false
var isTranslating = false
var panGestureRecognizerBeginBlock = { () -> Void in }
var panGestureRecognizerStopBlock = { (stopReason: LxTabBarControllerInteractionStopReason) -> Void in }
let _panToSwitchGestureRecognizer = UIPanGestureRecognizer()
var _interactiveTransition: UIPercentDrivenInteractiveTransition?
convenience init () {
self.init(nibName: nil, bundle: nil)
setup()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// setup()
}
func setup() {
self.delegate = self
_panToSwitchGestureRecognizer.addTarget(self, action: "panGestureRecognizerTriggerd:")
_panToSwitchGestureRecognizer.delegate = self
_panToSwitchGestureRecognizer.cancelsTouchesInView = false
_panToSwitchGestureRecognizer.maximumNumberOfTouches = 1
view.addGestureRecognizer(_panToSwitchGestureRecognizer)
}
func tabBarController(tabBarController: UITabBarController, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return animationController is Transition ? _interactiveTransition : nil
}
func tabBarController(tabBarController: UITabBarController, animationControllerForTransitionFromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return Transition()
}
func panGestureRecognizerTriggerd(pan: UIPanGestureRecognizer) {
var progress = pan.translationInView(pan.view!).x / pan.view!.bounds.size.width
if progress > 0 {
_switchType = .Last
}
else if progress < 0 {
_switchType = .Next
}
else {
_switchType = .Unknown
}
progress = abs(progress)
progress = max(0, progress)
progress = min(1, progress)
switch pan.state {
case .Began:
isTranslating = true
_interactiveTransition = UIPercentDrivenInteractiveTransition()
switch _switchType {
case .Last:
selectedIndex = max(0, selectedIndex - 1)
selectedViewController = viewControllers![selectedIndex] as? UIViewController
panGestureRecognizerBeginBlock()
case .Next:
selectedIndex = min(viewControllers!.count, selectedIndex + 1)
selectedViewController = viewControllers![selectedIndex] as? UIViewController
panGestureRecognizerBeginBlock()
case .Unknown:
break
}
case .Changed:
_interactiveTransition?.updateInteractiveTransition(CGFloat(progress))
case .Failed:
isTranslating = false
panGestureRecognizerStopBlock(.Failed)
default:
if abs(progress) > 0.5 {
_interactiveTransition?.finishInteractiveTransition()
panGestureRecognizerStopBlock(.Finished)
}
else {
_interactiveTransition?.cancelInteractiveTransition()
panGestureRecognizerStopBlock(.Cancelled)
}
_interactiveTransition = nil
isTranslating = false
}
}
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == _panToSwitchGestureRecognizer {
return !isTranslating
}
else {
return true
}
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == _panToSwitchGestureRecognizer || otherGestureRecognizer == _panToSwitchGestureRecognizer {
return recognizeOtherGestureSimultaneously
}
else {
return false
}
}
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
let viewControllerIndex = find(tabBarController.viewControllers as! [UIViewController], viewController)
if viewControllerIndex > selectedIndex {
_switchType = .Next
}
else if viewControllerIndex < selectedIndex {
_switchType = .Last
}
else {
_switchType = .Unknown
}
return true
}
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
NSNotificationCenter.defaultCenter().postNotificationName(LxTabBarControllerDidSelectViewControllerNotification, object: nil)
}
}