8
8
9
9
import UIKit
10
10
11
+ class ZLPanGestureRecognizer : UIPanGestureRecognizer {
12
+
13
+ }
14
+
11
15
public func == ( lhs: ZLSwipeableViewDirection , rhs: ZLSwipeableViewDirection ) -> Bool {
12
16
return lhs. rawValue == rhs. rawValue
13
17
}
@@ -115,7 +119,7 @@ public class ZLSwipeableView: UIView {
115
119
public var didStart : ( ( view: UIView , atLocation: CGPoint ) -> ( ) ) ?
116
120
public var swiping : ( ( view: UIView , atLocation: CGPoint , translation: CGPoint ) -> ( ) ) ?
117
121
public var didEnd : ( ( view: UIView , atLocation: CGPoint ) -> ( ) ) ?
118
- public var didSwipe : ( ( view: UIView , inDirection: ZLSwipeableViewDirection ) -> ( ) ) ?
122
+ public var didSwipe : ( ( view: UIView , inDirection: ZLSwipeableViewDirection , directionVector : CGVector ) -> ( ) ) ?
119
123
public var didCancel : ( ( view: UIView ) -> ( ) ) ?
120
124
121
125
// MARK: Swipe Control
@@ -145,18 +149,22 @@ public class ZLSwipeableView: UIView {
145
149
public func swipeTopView( inDirection direction: ZLSwipeableViewDirection ) {
146
150
if let topView = topView ( ) {
147
151
let ( location, directionVector) = interpretDirection ( topView: topView, direction: direction, views: views, swipeableView: self )
148
- swipeTopView ( fromPoint: location, inDirection: directionVector)
149
- didSwipe ? ( view: topView, inDirection: direction)
152
+ swipeTopView ( topView, direction: direction, location: location, directionVector: directionVector)
150
153
}
151
154
}
152
- public func swipeTopView( fromPoint location: CGPoint , inDirection direction : CGVector ) {
155
+ public func swipeTopView( fromPoint location: CGPoint , inDirection directionVector : CGVector ) {
153
156
if let topView = topView ( ) {
154
- unsnapView ( )
155
- pushView ( topView, fromPoint: location, inDirection: direction)
156
- removeFromViews ( topView)
157
- loadViews ( )
157
+ let direction = ZLSwipeableViewDirection . fromPoint ( CGPoint ( x: directionVector. dx, y: directionVector. dy) )
158
+ swipeTopView ( topView, direction: direction, location: location, directionVector: directionVector)
158
159
}
159
160
}
161
+ private func swipeTopView( topView: UIView , direction: ZLSwipeableViewDirection , location: CGPoint , directionVector: CGVector ) {
162
+ unsnapView ( )
163
+ pushView ( topView, fromPoint: location, inDirection: directionVector)
164
+ removeFromViews ( topView)
165
+ loadViews ( )
166
+ didSwipe ? ( view: topView, inDirection: direction, directionVector: directionVector)
167
+ }
160
168
161
169
// MARK: View Management
162
170
private var views = [ UIView] ( )
@@ -262,6 +270,14 @@ public class ZLSwipeableView: UIView {
262
270
addSubview ( anchorContainerView)
263
271
}
264
272
273
+ deinit {
274
+ timer? . invalidate ( )
275
+ animator. removeAllBehaviors ( )
276
+ pushAnimator. removeAllBehaviors ( )
277
+ views. removeAll ( )
278
+ pushBehaviors. removeAll ( )
279
+ }
280
+
265
281
override public func layoutSubviews( ) {
266
282
super. layoutSubviews ( )
267
283
containerView. frame = bounds
@@ -276,17 +292,17 @@ public class ZLSwipeableView: UIView {
276
292
func handlePan( recognizer: UIPanGestureRecognizer ) {
277
293
let translation = recognizer. translationInView ( self )
278
294
let location = recognizer. locationInView ( self )
279
- let aView = recognizer. view!
295
+ let topView = recognizer. view!
280
296
281
297
switch recognizer. state {
282
298
case . Began:
283
299
unsnapView ( )
284
- attachView ( aView , toPoint: location)
285
- didStart ? ( view: aView , atLocation: location)
300
+ attachView ( topView , toPoint: location)
301
+ didStart ? ( view: topView , atLocation: location)
286
302
case . Changed:
287
303
unsnapView ( )
288
- attachView ( aView , toPoint: location)
289
- swiping ? ( view: aView , atLocation: location, translation: translation)
304
+ attachView ( topView , toPoint: location)
305
+ swiping ? ( view: topView , atLocation: location, translation: translation)
290
306
case . Ended, . Cancelled:
291
307
detachView ( )
292
308
let velocity = recognizer. velocityInView ( self )
@@ -301,16 +317,18 @@ public class ZLSwipeableView: UIView {
301
317
let normalizedTrans = translation. normalized
302
318
let throwVelocity = max ( velocityMag, velocityThreshold)
303
319
let directionVector = CGVector ( dx: normalizedTrans. x*throwVelocity, dy: normalizedTrans. y*throwVelocity)
320
+
321
+ swipeTopView ( topView, direction: direction, location: location, directionVector: directionVector)
304
322
305
- pushView ( aView , fromPoint: location, inDirection: directionVector)
306
- removeFromViews ( aView )
307
- didSwipe ? ( view: aView , inDirection: ZLSwipeableViewDirection . fromPoint ( translation) )
308
- loadViews ( )
323
+ // pushView(topView , fromPoint: location, inDirection: directionVector)
324
+ // removeFromViews(topView )
325
+ // didSwipe?(view: topView , inDirection: ZLSwipeableViewDirection.fromPoint(translation))
326
+ // loadViews()
309
327
} else {
310
- snapView ( aView , toPoint: convertPoint ( center, fromView: superview) )
311
- didCancel ? ( view: aView )
328
+ snapView ( topView , toPoint: convertPoint ( center, fromView: superview) )
329
+ didCancel ? ( view: topView )
312
330
}
313
- didEnd ? ( view: aView , atLocation: location)
331
+ didEnd ? ( view: topView , atLocation: location)
314
332
default :
315
333
break
316
334
}
@@ -376,7 +394,7 @@ public class ZLSwipeableView: UIView {
376
394
private func cleanUpWithPredicate( predicate: ( UIView) - > Bool) - > [ Int] {
377
395
var indexes = [ Int] ( )
378
396
for i in 0 ..< pushBehaviors. count {
379
- let ( anchorView, aView, attachment, push) = self . pushBehaviors [ i]
397
+ let ( anchorView, aView, attachment, push) = pushBehaviors [ i]
380
398
if predicate ( aView) {
381
399
anchorView. removeFromSuperview ( )
382
400
removeFromContainerView ( aView)
@@ -399,9 +417,8 @@ public class ZLSwipeableView: UIView {
399
417
anchorView. hidden = true
400
418
anchorContainerView. addSubview ( anchorView)
401
419
402
- var point = point
403
420
let p = aView. convertPoint ( aView. center, fromView: aView. superview)
404
- point = aView. convertPoint ( point, fromView: aView. superview)
421
+ var point = aView. convertPoint ( point, fromView: aView. superview)
405
422
var attachmentViewToAnchorView = UIAttachmentBehavior ( item: aView, offsetFromCenter: UIOffset ( horizontal: - ( p. x - point. x) , vertical: - ( p. y - point. y) ) , attachedToItem: anchorView, offsetFromCenter: UIOffsetZero)
406
423
attachmentViewToAnchorView!. length = 0
407
424
0 commit comments