@@ -13,26 +13,26 @@ class ViewManager : NSObject {
13
13
// Snapping -> [Moving]+ -> Snapping
14
14
// Snapping -> [Moving]+ -> Swiping -> Snapping
15
15
enum State {
16
- case Snapping ( CGPoint ) , Moving ( CGPoint ) , Swiping ( CGPoint , CGVector )
16
+ case snapping ( CGPoint ) , moving ( CGPoint ) , swiping ( CGPoint , CGVector )
17
17
}
18
18
19
19
var state : State {
20
20
didSet {
21
- if case . Snapping ( _) = oldValue, case let . Moving ( point) = state {
21
+ if case . snapping ( _) = oldValue, case let . moving ( point) = state {
22
22
unsnapView ( )
23
23
attachView ( toPoint: point)
24
- } else if case . Snapping ( _) = oldValue, case let . Swiping ( origin, direction) = state {
24
+ } else if case . snapping ( _) = oldValue, case let . swiping ( origin, direction) = state {
25
25
unsnapView ( )
26
26
attachView ( toPoint: origin)
27
27
pushView ( fromPoint: origin, inDirection: direction)
28
- } else if case . Moving ( _) = oldValue, case let . Moving ( point) = state {
28
+ } else if case . moving ( _) = oldValue, case let . moving ( point) = state {
29
29
moveView ( toPoint: point)
30
- } else if case . Moving ( _) = oldValue, case let . Snapping ( point) = state {
30
+ } else if case . moving ( _) = oldValue, case let . snapping ( point) = state {
31
31
detachView ( )
32
32
snapView ( point)
33
- } else if case . Moving ( _) = oldValue, case let . Swiping ( origin, direction) = state {
33
+ } else if case . moving ( _) = oldValue, case let . swiping ( origin, direction) = state {
34
34
pushView ( fromPoint: origin, inDirection: direction)
35
- } else if case . Swiping ( _, _) = oldValue, case let . Snapping ( point) = state {
35
+ } else if case . swiping ( _, _) = oldValue, case let . snapping ( point) = state {
36
36
unpushView ( )
37
37
detachView ( )
38
38
snapView ( point)
@@ -41,22 +41,22 @@ class ViewManager : NSObject {
41
41
}
42
42
43
43
/// To be added to view and removed
44
- private class ZLPanGestureRecognizer : UIPanGestureRecognizer { }
45
- private class ZLTapGestureRecognizer : UITapGestureRecognizer { }
44
+ fileprivate class ZLPanGestureRecognizer : UIPanGestureRecognizer { }
45
+ fileprivate class ZLTapGestureRecognizer : UITapGestureRecognizer { }
46
46
47
- static private let anchorViewWidth = CGFloat ( 1000 )
48
- private var anchorView = UIView ( frame: CGRect ( x: 0 , y: 0 , width: anchorViewWidth, height: anchorViewWidth) )
47
+ static fileprivate let anchorViewWidth = CGFloat ( 1000 )
48
+ fileprivate var anchorView = UIView ( frame: CGRect ( x: 0 , y: 0 , width: anchorViewWidth, height: anchorViewWidth) )
49
49
50
- private var snapBehavior : UISnapBehavior !
51
- private var viewToAnchorViewAttachmentBehavior : UIAttachmentBehavior !
52
- private var anchorViewToPointAttachmentBehavior : UIAttachmentBehavior !
53
- private var pushBehavior : UIPushBehavior !
50
+ fileprivate var snapBehavior : UISnapBehavior !
51
+ fileprivate var viewToAnchorViewAttachmentBehavior : UIAttachmentBehavior !
52
+ fileprivate var anchorViewToPointAttachmentBehavior : UIAttachmentBehavior !
53
+ fileprivate var pushBehavior : UIPushBehavior !
54
54
55
- private let view : UIView
56
- private let containerView : UIView
57
- private let miscContainerView : UIView
58
- private let animator : UIDynamicAnimator
59
- private weak var swipeableView : ZLSwipeableView ?
55
+ fileprivate let view : UIView
56
+ fileprivate let containerView : UIView
57
+ fileprivate let miscContainerView : UIView
58
+ fileprivate let animator : UIDynamicAnimator
59
+ fileprivate weak var swipeableView : ZLSwipeableView ?
60
60
61
61
init ( view: UIView , containerView: UIView , index: Int , miscContainerView: UIView , animator: UIDynamicAnimator , swipeableView: ZLSwipeableView ) {
62
62
self . view = view
@@ -71,16 +71,16 @@ class ViewManager : NSObject {
71
71
view. addGestureRecognizer ( ZLPanGestureRecognizer ( target: self , action: #selector( ViewManager . handlePan ( _: ) ) ) )
72
72
view. addGestureRecognizer ( ZLTapGestureRecognizer ( target: self , action: #selector( ViewManager . handleTap ( _: ) ) ) )
73
73
miscContainerView. addSubview ( anchorView)
74
- containerView. insertSubview ( view, atIndex : index)
74
+ containerView. insertSubview ( view, at : index)
75
75
}
76
76
77
- static func defaultSnappingState( view: UIView ) -> State {
78
- return . Snapping ( view. convertPoint ( view. center, fromView : view. superview) )
77
+ static func defaultSnappingState( _ view: UIView ) -> State {
78
+ return . snapping ( view. convert ( view. center, from : view. superview) )
79
79
}
80
80
81
81
func snappingStateAtContainerCenter( ) -> State {
82
82
guard let swipeableView = swipeableView else { return ViewManager . defaultSnappingState ( view) }
83
- return . Snapping ( containerView. convertPoint ( swipeableView. center, fromView : swipeableView. superview) )
83
+ return . snapping ( containerView. convert ( swipeableView. center, from : swipeableView. superview) )
84
84
}
85
85
86
86
deinit {
@@ -98,7 +98,7 @@ class ViewManager : NSObject {
98
98
}
99
99
100
100
for gestureRecognizer in view. gestureRecognizers! {
101
- if gestureRecognizer. isKindOfClass ( ZLPanGestureRecognizer . classForCoder ( ) ) {
101
+ if gestureRecognizer. isKind ( of : ZLPanGestureRecognizer . classForCoder ( ) ) {
102
102
view. removeGestureRecognizer ( gestureRecognizer)
103
103
}
104
104
}
@@ -107,108 +107,108 @@ class ViewManager : NSObject {
107
107
view. removeFromSuperview ( )
108
108
}
109
109
110
- func handlePan( recognizer: UIPanGestureRecognizer ) {
110
+ func handlePan( _ recognizer: UIPanGestureRecognizer ) {
111
111
guard let swipeableView = swipeableView else { return }
112
112
113
- let translation = recognizer. translationInView ( containerView)
114
- let location = recognizer. locationInView ( containerView)
115
- let velocity = recognizer. velocityInView ( containerView)
113
+ let translation = recognizer. translation ( in : containerView)
114
+ let location = recognizer. location ( in : containerView)
115
+ let velocity = recognizer. velocity ( in : containerView)
116
116
let movement = Movement ( location: location, translation: translation, velocity: velocity)
117
117
118
118
switch recognizer. state {
119
- case . Began :
120
- guard case . Snapping ( _) = state else { return }
121
- state = . Moving ( location)
122
- swipeableView. didStart ? ( view: view , atLocation : location)
123
- case . Changed :
124
- guard case . Moving ( _) = state else { return }
125
- state = . Moving ( location)
126
- swipeableView. swiping ? ( view: view , atLocation : location, translation : translation)
127
- case . Ended , . Cancelled :
128
- guard case . Moving ( _) = state else { return }
129
- if swipeableView. shouldSwipeView ( view: view , movement: movement , swipeableView : swipeableView) {
119
+ case . began :
120
+ guard case . snapping ( _) = state else { return }
121
+ state = . moving ( location)
122
+ swipeableView. didStart ? ( view, location)
123
+ case . changed :
124
+ guard case . moving ( _) = state else { return }
125
+ state = . moving ( location)
126
+ swipeableView. swiping ? ( view, location, translation)
127
+ case . ended , . cancelled :
128
+ guard case . moving ( _) = state else { return }
129
+ if swipeableView. shouldSwipeView ( view, movement, swipeableView) {
130
130
let directionVector = CGVector ( point: translation. normalized * max( velocity. magnitude, swipeableView. minVelocityInPointPerSecond) )
131
- state = . Swiping ( location, directionVector)
131
+ state = . swiping ( location, directionVector)
132
132
swipeableView. swipeView ( view, location: location, directionVector: directionVector)
133
133
} else {
134
134
state = snappingStateAtContainerCenter ( )
135
- swipeableView. didCancel ? ( view: view )
135
+ swipeableView. didCancel ? ( view)
136
136
}
137
- swipeableView. didEnd ? ( view: view , atLocation : location)
137
+ swipeableView. didEnd ? ( view, location)
138
138
default :
139
139
break
140
140
}
141
141
}
142
142
143
- func handleTap( recognizer: UITapGestureRecognizer ) {
144
- guard let swipeableView = swipeableView, topView = swipeableView. topView ( ) else { return }
143
+ func handleTap( _ recognizer: UITapGestureRecognizer ) {
144
+ guard let swipeableView = swipeableView, let topView = swipeableView. topView ( ) else { return }
145
145
146
- let location = recognizer. locationInView ( containerView)
147
- swipeableView. didTap ? ( view : topView, atLocation : location)
146
+ let location = recognizer. location ( in : containerView)
147
+ swipeableView. didTap ? ( topView, location)
148
148
}
149
149
150
- private func snapView( point: CGPoint ) {
151
- snapBehavior = UISnapBehavior ( item: view, snapToPoint : point)
150
+ fileprivate func snapView( _ point: CGPoint ) {
151
+ snapBehavior = UISnapBehavior ( item: view, snapTo : point)
152
152
snapBehavior!. damping = 0.75
153
153
addBehavior ( snapBehavior)
154
154
}
155
155
156
- private func unsnapView( ) {
156
+ fileprivate func unsnapView( ) {
157
157
guard let snapBehavior = snapBehavior else { return }
158
158
removeBehavior ( snapBehavior)
159
159
}
160
160
161
- private func attachView( toPoint point: CGPoint ) {
161
+ fileprivate func attachView( toPoint point: CGPoint ) {
162
162
anchorView. center = point
163
- anchorView. backgroundColor = UIColor . blueColor ( )
164
- anchorView. hidden = true
163
+ anchorView. backgroundColor = UIColor . blue
164
+ anchorView. isHidden = true
165
165
166
166
// attach aView to anchorView
167
167
let p = view. center
168
- viewToAnchorViewAttachmentBehavior = UIAttachmentBehavior ( item: view, offsetFromCenter: UIOffset ( horizontal: - ( p. x - point. x) , vertical: - ( p. y - point. y) ) , attachedToItem : anchorView, offsetFromCenter: UIOffsetZero )
168
+ viewToAnchorViewAttachmentBehavior = UIAttachmentBehavior ( item: view, offsetFromCenter: UIOffset ( horizontal: - ( p. x - point. x) , vertical: - ( p. y - point. y) ) , attachedTo : anchorView, offsetFromCenter: UIOffset . zero )
169
169
viewToAnchorViewAttachmentBehavior!. length = 0
170
170
171
171
// attach anchorView to point
172
- anchorViewToPointAttachmentBehavior = UIAttachmentBehavior ( item: anchorView, offsetFromCenter: UIOffsetZero , attachedToAnchor: point)
172
+ anchorViewToPointAttachmentBehavior = UIAttachmentBehavior ( item: anchorView, offsetFromCenter: UIOffset . zero , attachedToAnchor: point)
173
173
anchorViewToPointAttachmentBehavior!. damping = 100
174
174
anchorViewToPointAttachmentBehavior!. length = 0
175
175
176
176
addBehavior ( viewToAnchorViewAttachmentBehavior!)
177
177
addBehavior ( anchorViewToPointAttachmentBehavior!)
178
178
}
179
179
180
- private func moveView( toPoint point: CGPoint ) {
180
+ fileprivate func moveView( toPoint point: CGPoint ) {
181
181
guard let _ = viewToAnchorViewAttachmentBehavior, let toPoint = anchorViewToPointAttachmentBehavior else { return }
182
182
toPoint. anchorPoint = point
183
183
}
184
184
185
- private func detachView( ) {
185
+ fileprivate func detachView( ) {
186
186
guard let viewToAnchorViewAttachmentBehavior = viewToAnchorViewAttachmentBehavior, let anchorViewToPointAttachmentBehavior = anchorViewToPointAttachmentBehavior else { return }
187
187
removeBehavior ( viewToAnchorViewAttachmentBehavior)
188
188
removeBehavior ( anchorViewToPointAttachmentBehavior)
189
189
}
190
190
191
- private func pushView( fromPoint point: CGPoint , inDirection direction: CGVector ) {
191
+ fileprivate func pushView( fromPoint point: CGPoint , inDirection direction: CGVector ) {
192
192
guard let _ = viewToAnchorViewAttachmentBehavior, let anchorViewToPointAttachmentBehavior = anchorViewToPointAttachmentBehavior else { return }
193
193
194
194
removeBehavior ( anchorViewToPointAttachmentBehavior)
195
195
196
- pushBehavior = UIPushBehavior ( items: [ anchorView] , mode: . Instantaneous )
196
+ pushBehavior = UIPushBehavior ( items: [ anchorView] , mode: . instantaneous )
197
197
pushBehavior. pushDirection = direction
198
198
addBehavior ( pushBehavior)
199
199
}
200
200
201
- private func unpushView( ) {
201
+ fileprivate func unpushView( ) {
202
202
guard let pushBehavior = pushBehavior else { return }
203
203
removeBehavior ( pushBehavior)
204
204
}
205
205
206
- private func addBehavior( behavior: UIDynamicBehavior ) {
206
+ fileprivate func addBehavior( _ behavior: UIDynamicBehavior ) {
207
207
animator. addBehavior ( behavior)
208
208
}
209
209
210
- private func removeBehavior( behavior: UIDynamicBehavior ) {
210
+ fileprivate func removeBehavior( _ behavior: UIDynamicBehavior ) {
211
211
animator. removeBehavior ( behavior)
212
212
}
213
213
214
- }
214
+ }
0 commit comments