Skip to content

Commit 792ce4a

Browse files
committed
bug fixes, update didSwipe
1 parent 7295dae commit 792ce4a

File tree

5 files changed

+43
-43
lines changed

5 files changed

+43
-43
lines changed

ZLSwipeableViewSwift/ZLPanGestureRecognizer.swift

-13
This file was deleted.

ZLSwipeableViewSwift/ZLSwipeableView.swift

+40-23
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
import UIKit
1010

11+
class ZLPanGestureRecognizer: UIPanGestureRecognizer {
12+
13+
}
14+
1115
public func ==(lhs: ZLSwipeableViewDirection, rhs: ZLSwipeableViewDirection) -> Bool {
1216
return lhs.rawValue == rhs.rawValue
1317
}
@@ -115,7 +119,7 @@ public class ZLSwipeableView: UIView {
115119
public var didStart: ((view: UIView, atLocation: CGPoint) -> ())?
116120
public var swiping: ((view: UIView, atLocation: CGPoint, translation: CGPoint) -> ())?
117121
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) -> ())?
119123
public var didCancel: ((view: UIView) -> ())?
120124

121125
// MARK: Swipe Control
@@ -145,18 +149,22 @@ public class ZLSwipeableView: UIView {
145149
public func swipeTopView(inDirection direction: ZLSwipeableViewDirection) {
146150
if let topView = topView() {
147151
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)
150153
}
151154
}
152-
public func swipeTopView(fromPoint location: CGPoint, inDirection direction: CGVector) {
155+
public func swipeTopView(fromPoint location: CGPoint, inDirection directionVector: CGVector) {
153156
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)
158159
}
159160
}
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+
}
160168

161169
// MARK: View Management
162170
private var views = [UIView]()
@@ -262,6 +270,14 @@ public class ZLSwipeableView: UIView {
262270
addSubview(anchorContainerView)
263271
}
264272

273+
deinit {
274+
timer?.invalidate()
275+
animator.removeAllBehaviors()
276+
pushAnimator.removeAllBehaviors()
277+
views.removeAll()
278+
pushBehaviors.removeAll()
279+
}
280+
265281
override public func layoutSubviews() {
266282
super.layoutSubviews()
267283
containerView.frame = bounds
@@ -276,17 +292,17 @@ public class ZLSwipeableView: UIView {
276292
func handlePan(recognizer: UIPanGestureRecognizer) {
277293
let translation = recognizer.translationInView(self)
278294
let location = recognizer.locationInView(self)
279-
let aView = recognizer.view!
295+
let topView = recognizer.view!
280296

281297
switch recognizer.state {
282298
case .Began:
283299
unsnapView()
284-
attachView(aView, toPoint: location)
285-
didStart?(view: aView, atLocation: location)
300+
attachView(topView, toPoint: location)
301+
didStart?(view: topView, atLocation: location)
286302
case .Changed:
287303
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)
290306
case .Ended, .Cancelled:
291307
detachView()
292308
let velocity = recognizer.velocityInView(self)
@@ -301,16 +317,18 @@ public class ZLSwipeableView: UIView {
301317
let normalizedTrans = translation.normalized
302318
let throwVelocity = max(velocityMag, velocityThreshold)
303319
let directionVector = CGVector(dx: normalizedTrans.x*throwVelocity, dy: normalizedTrans.y*throwVelocity)
320+
321+
swipeTopView(topView, direction: direction, location: location, directionVector: directionVector)
304322

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()
309327
} 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)
312330
}
313-
didEnd?(view: aView, atLocation: location)
331+
didEnd?(view: topView, atLocation: location)
314332
default:
315333
break
316334
}
@@ -376,7 +394,7 @@ public class ZLSwipeableView: UIView {
376394
private func cleanUpWithPredicate(predicate: (UIView) -> Bool) -> [Int] {
377395
var indexes = [Int]()
378396
for i in 0..<pushBehaviors.count {
379-
let (anchorView, aView, attachment, push) = self.pushBehaviors[i]
397+
let (anchorView, aView, attachment, push) = pushBehaviors[i]
380398
if predicate(aView) {
381399
anchorView.removeFromSuperview()
382400
removeFromContainerView(aView)
@@ -399,9 +417,8 @@ public class ZLSwipeableView: UIView {
399417
anchorView.hidden = true
400418
anchorContainerView.addSubview(anchorView)
401419

402-
var point = point
403420
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)
405422
var attachmentViewToAnchorView = UIAttachmentBehavior(item: aView, offsetFromCenter: UIOffset(horizontal: -(p.x - point.x), vertical: -(p.y - point.y)), attachedToItem: anchorView, offsetFromCenter: UIOffsetZero)
406423
attachmentViewToAnchorView!.length = 0
407424

ZLSwipeableViewSwiftDemo/ZLSwipeableViewSwiftDemo.xcodeproj/project.pbxproj

-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
09673A9A1AEF5B2E008C7240 /* ZLSwipeableViewSwiftDemoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09673A991AEF5B2E008C7240 /* ZLSwipeableViewSwiftDemoTests.swift */; };
1515
098070A21B12ECAD00C90479 /* CardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 098070A11B12ECAD00C90479 /* CardView.swift */; };
1616
098070A41B12ED6400C90479 /* CardContentView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 098070A31B12ED6400C90479 /* CardContentView.xib */; };
17-
098070A81B12EDF400C90479 /* ZLPanGestureRecognizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 098070A61B12EDF400C90479 /* ZLPanGestureRecognizer.swift */; };
1817
098070A91B12EDF400C90479 /* ZLSwipeableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 098070A71B12EDF400C90479 /* ZLSwipeableView.swift */; };
1918
098070AB1B13D69300C90479 /* MenuTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 098070AA1B13D69300C90479 /* MenuTableViewController.swift */; };
2019
098070AD1B13DEA600C90479 /* UndoDemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 098070AC1B13DEA600C90479 /* UndoDemoViewController.swift */; };
@@ -47,7 +46,6 @@
4746
09673A991AEF5B2E008C7240 /* ZLSwipeableViewSwiftDemoTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZLSwipeableViewSwiftDemoTests.swift; sourceTree = "<group>"; };
4847
098070A11B12ECAD00C90479 /* CardView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CardView.swift; sourceTree = "<group>"; };
4948
098070A31B12ED6400C90479 /* CardContentView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = CardContentView.xib; sourceTree = "<group>"; };
50-
098070A61B12EDF400C90479 /* ZLPanGestureRecognizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ZLPanGestureRecognizer.swift; sourceTree = "<group>"; };
5149
098070A71B12EDF400C90479 /* ZLSwipeableView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ZLSwipeableView.swift; sourceTree = "<group>"; };
5250
098070AA1B13D69300C90479 /* MenuTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MenuTableViewController.swift; sourceTree = "<group>"; };
5351
098070AC1B13DEA600C90479 /* UndoDemoViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UndoDemoViewController.swift; sourceTree = "<group>"; };
@@ -155,7 +153,6 @@
155153
098070A51B12EDF400C90479 /* ZLSwipeableViewSwift */ = {
156154
isa = PBXGroup;
157155
children = (
158-
098070A61B12EDF400C90479 /* ZLPanGestureRecognizer.swift */,
159156
098070A71B12EDF400C90479 /* ZLSwipeableView.swift */,
160157
);
161158
name = ZLSwipeableViewSwift;
@@ -379,7 +376,6 @@
379376
098070AB1B13D69300C90479 /* MenuTableViewController.swift in Sources */,
380377
09673A841AEF5B2E008C7240 /* AppDelegate.swift in Sources */,
381378
098070A21B12ECAD00C90479 /* CardView.swift in Sources */,
382-
098070A81B12EDF400C90479 /* ZLPanGestureRecognizer.swift in Sources */,
383379
);
384380
runOnlyForDeploymentPostprocessing = 0;
385381
};

ZLSwipeableViewSwiftDemo/ZLSwipeableViewSwiftDemo/UndoDemoViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class UndoDemoViewController: ZLSwipeableViewController {
1414
override func viewDidLoad() {
1515
super.viewDidLoad()
1616
var lastSwipedView: (UIView, ZLSwipeableViewDirection)?
17-
swipeableView.didSwipe = {view, direction in
17+
swipeableView.didSwipe = {view, direction, vector in
1818
println("Did swipe view in direction: \(direction)")
1919
lastSwipedView = (view, direction)
2020
}

ZLSwipeableViewSwiftDemo/ZLSwipeableViewSwiftDemo/ZLSwipeableViewController.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ class ZLSwipeableViewController: UIViewController {
9393
swipeableView.didEnd = {view, location in
9494
println("Did end swiping view at location: \(location)")
9595
}
96-
swipeableView.didSwipe = {view, direction in
97-
println("Did swipe view in direction: \(direction)")
96+
swipeableView.didSwipe = {view, direction, vector in
97+
println("Did swipe view in direction: \(direction), vector: \(vector)")
9898
}
9999
swipeableView.didCancel = {view in
100100
println("Did cancel swiping view")

0 commit comments

Comments
 (0)