Skip to content

Commit 8deffbe

Browse files
authored
Merge pull request #82 from zhxnlai/swift3
Swift 3.0 Compatibility
2 parents 7b62c83 + 48ef9b0 commit 8deffbe

File tree

6 files changed

+164
-154
lines changed

6 files changed

+164
-154
lines changed

ZLSwipeableViewSwift/Direction.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public func ==(lhs: Direction, rhs: Direction) -> Bool {
1818
/**
1919
* Swiped direction.
2020
*/
21-
public struct Direction : OptionSetType, CustomStringConvertible {
21+
public struct Direction : OptionSet, CustomStringConvertible {
2222

2323
public var rawValue: UInt
2424

@@ -35,7 +35,7 @@ public struct Direction : OptionSetType, CustomStringConvertible {
3535
public static let Vertical: Direction = [Up, Down]
3636
public static let All: Direction = [Horizontal, Vertical]
3737

38-
public static func fromPoint(point: CGPoint) -> Direction {
38+
public static func fromPoint(_ point: CGPoint) -> Direction {
3939
switch (point.x, point.y) {
4040
case let (x, y) where abs(x) >= abs(y) && x > 0:
4141
return .Right
@@ -73,4 +73,4 @@ public struct Direction : OptionSetType, CustomStringConvertible {
7373
}
7474
}
7575

76-
}
76+
}

ZLSwipeableViewSwift/Scheduler.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ class Scheduler : NSObject {
1313
typealias Action = () -> Void
1414
typealias EndCondition = () -> Bool
1515

16-
var timer: NSTimer?
16+
var timer: Timer?
1717
var action: Action?
1818
var endCondition: EndCondition?
1919

20-
func scheduleRepeatedly(action: Action, interval: NSTimeInterval, endCondition: EndCondition) {
20+
func scheduleRepeatedly(_ action: @escaping Action, interval: TimeInterval, endCondition: @escaping EndCondition) {
2121
guard timer == nil && interval > 0 else { return }
2222
self.action = action
2323
self.endCondition = endCondition
24-
timer = NSTimer.scheduledTimerWithTimeInterval(interval, target: self, selector: #selector(Scheduler.doAction(_:)), userInfo: nil, repeats: true)
24+
timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(Scheduler.doAction(_:)), userInfo: nil, repeats: true)
2525
}
2626

27-
func doAction(timer: NSTimer) {
28-
guard let action = action, let endCondition = endCondition where !endCondition() else {
27+
func doAction(_ timer: Timer) {
28+
guard let action = action, let endCondition = endCondition , !endCondition() else {
2929
timer.invalidate()
3030
self.timer = nil
3131
self.action = nil
@@ -35,4 +35,4 @@ class Scheduler : NSObject {
3535
action()
3636
}
3737

38-
}
38+
}

ZLSwipeableViewSwift/ViewManager.swift

+63-63
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ class ViewManager : NSObject {
1313
// Snapping -> [Moving]+ -> Snapping
1414
// Snapping -> [Moving]+ -> Swiping -> Snapping
1515
enum State {
16-
case Snapping(CGPoint), Moving(CGPoint), Swiping(CGPoint, CGVector)
16+
case snapping(CGPoint), moving(CGPoint), swiping(CGPoint, CGVector)
1717
}
1818

1919
var state: State {
2020
didSet {
21-
if case .Snapping(_) = oldValue, case let .Moving(point) = state {
21+
if case .snapping(_) = oldValue, case let .moving(point) = state {
2222
unsnapView()
2323
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 {
2525
unsnapView()
2626
attachView(toPoint: origin)
2727
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 {
2929
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 {
3131
detachView()
3232
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 {
3434
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 {
3636
unpushView()
3737
detachView()
3838
snapView(point)
@@ -41,22 +41,22 @@ class ViewManager : NSObject {
4141
}
4242

4343
/// 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 { }
4646

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))
4949

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!
5454

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?
6060

6161
init(view: UIView, containerView: UIView, index: Int, miscContainerView: UIView, animator: UIDynamicAnimator, swipeableView: ZLSwipeableView) {
6262
self.view = view
@@ -71,16 +71,16 @@ class ViewManager : NSObject {
7171
view.addGestureRecognizer(ZLPanGestureRecognizer(target: self, action: #selector(ViewManager.handlePan(_:))))
7272
view.addGestureRecognizer(ZLTapGestureRecognizer(target: self, action: #selector(ViewManager.handleTap(_:))))
7373
miscContainerView.addSubview(anchorView)
74-
containerView.insertSubview(view, atIndex: index)
74+
containerView.insertSubview(view, at: index)
7575
}
7676

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))
7979
}
8080

8181
func snappingStateAtContainerCenter() -> State {
8282
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))
8484
}
8585

8686
deinit {
@@ -98,7 +98,7 @@ class ViewManager : NSObject {
9898
}
9999

100100
for gestureRecognizer in view.gestureRecognizers! {
101-
if gestureRecognizer.isKindOfClass(ZLPanGestureRecognizer.classForCoder()) {
101+
if gestureRecognizer.isKind(of: ZLPanGestureRecognizer.classForCoder()) {
102102
view.removeGestureRecognizer(gestureRecognizer)
103103
}
104104
}
@@ -107,108 +107,108 @@ class ViewManager : NSObject {
107107
view.removeFromSuperview()
108108
}
109109

110-
func handlePan(recognizer: UIPanGestureRecognizer) {
110+
func handlePan(_ recognizer: UIPanGestureRecognizer) {
111111
guard let swipeableView = swipeableView else { return }
112112

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)
116116
let movement = Movement(location: location, translation: translation, velocity: velocity)
117117

118118
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) {
130130
let directionVector = CGVector(point: translation.normalized * max(velocity.magnitude, swipeableView.minVelocityInPointPerSecond))
131-
state = .Swiping(location, directionVector)
131+
state = .swiping(location, directionVector)
132132
swipeableView.swipeView(view, location: location, directionVector: directionVector)
133133
} else {
134134
state = snappingStateAtContainerCenter()
135-
swipeableView.didCancel?(view: view)
135+
swipeableView.didCancel?(view)
136136
}
137-
swipeableView.didEnd?(view: view, atLocation: location)
137+
swipeableView.didEnd?(view, location)
138138
default:
139139
break
140140
}
141141
}
142142

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 }
145145

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)
148148
}
149149

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)
152152
snapBehavior!.damping = 0.75
153153
addBehavior(snapBehavior)
154154
}
155155

156-
private func unsnapView() {
156+
fileprivate func unsnapView() {
157157
guard let snapBehavior = snapBehavior else { return }
158158
removeBehavior(snapBehavior)
159159
}
160160

161-
private func attachView(toPoint point: CGPoint) {
161+
fileprivate func attachView(toPoint point: CGPoint) {
162162
anchorView.center = point
163-
anchorView.backgroundColor = UIColor.blueColor()
164-
anchorView.hidden = true
163+
anchorView.backgroundColor = UIColor.blue
164+
anchorView.isHidden = true
165165

166166
// attach aView to anchorView
167167
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)
169169
viewToAnchorViewAttachmentBehavior!.length = 0
170170

171171
// attach anchorView to point
172-
anchorViewToPointAttachmentBehavior = UIAttachmentBehavior(item: anchorView, offsetFromCenter: UIOffsetZero, attachedToAnchor: point)
172+
anchorViewToPointAttachmentBehavior = UIAttachmentBehavior(item: anchorView, offsetFromCenter: UIOffset.zero, attachedToAnchor: point)
173173
anchorViewToPointAttachmentBehavior!.damping = 100
174174
anchorViewToPointAttachmentBehavior!.length = 0
175175

176176
addBehavior(viewToAnchorViewAttachmentBehavior!)
177177
addBehavior(anchorViewToPointAttachmentBehavior!)
178178
}
179179

180-
private func moveView(toPoint point: CGPoint) {
180+
fileprivate func moveView(toPoint point: CGPoint) {
181181
guard let _ = viewToAnchorViewAttachmentBehavior, let toPoint = anchorViewToPointAttachmentBehavior else { return }
182182
toPoint.anchorPoint = point
183183
}
184184

185-
private func detachView() {
185+
fileprivate func detachView() {
186186
guard let viewToAnchorViewAttachmentBehavior = viewToAnchorViewAttachmentBehavior, let anchorViewToPointAttachmentBehavior = anchorViewToPointAttachmentBehavior else { return }
187187
removeBehavior(viewToAnchorViewAttachmentBehavior)
188188
removeBehavior(anchorViewToPointAttachmentBehavior)
189189
}
190190

191-
private func pushView(fromPoint point: CGPoint, inDirection direction: CGVector) {
191+
fileprivate func pushView(fromPoint point: CGPoint, inDirection direction: CGVector) {
192192
guard let _ = viewToAnchorViewAttachmentBehavior, let anchorViewToPointAttachmentBehavior = anchorViewToPointAttachmentBehavior else { return }
193193

194194
removeBehavior(anchorViewToPointAttachmentBehavior)
195195

196-
pushBehavior = UIPushBehavior(items: [anchorView], mode: .Instantaneous)
196+
pushBehavior = UIPushBehavior(items: [anchorView], mode: .instantaneous)
197197
pushBehavior.pushDirection = direction
198198
addBehavior(pushBehavior)
199199
}
200200

201-
private func unpushView() {
201+
fileprivate func unpushView() {
202202
guard let pushBehavior = pushBehavior else { return }
203203
removeBehavior(pushBehavior)
204204
}
205205

206-
private func addBehavior(behavior: UIDynamicBehavior) {
206+
fileprivate func addBehavior(_ behavior: UIDynamicBehavior) {
207207
animator.addBehavior(behavior)
208208
}
209209

210-
private func removeBehavior(behavior: UIDynamicBehavior) {
210+
fileprivate func removeBehavior(_ behavior: UIDynamicBehavior) {
211211
animator.removeBehavior(behavior)
212212
}
213213

214-
}
214+
}

0 commit comments

Comments
 (0)