Skip to content

Commit fb0d92d

Browse files
authored
Merge pull request #11 from mgopsill/add-long-press-support
Add Support for UILongPressGestureRecognizer
2 parents d0be4d2 + 4bb1893 commit fb0d92d

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

Example/SpecTools/ViewController.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ViewController: UIViewController {
1919
let label3 = UILabel()
2020

2121
let button1 = UIButton()
22+
let longPressButton = UIButton()
2223

2324

2425
// MARK: Test properties
@@ -86,6 +87,21 @@ class ViewController: UIViewController {
8687
make.right.equalTo(-20)
8788
make.height.equalTo(36)
8889
}
90+
91+
longPressButton.setTitle("Long press button", for: .normal)
92+
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
93+
longPressButton.addGestureRecognizer(longPressGesture)
94+
longPressButton.layer.borderColor = UIColor.lightGray.cgColor
95+
longPressButton.layer.borderWidth = 1
96+
longPressButton.layer.cornerRadius = 5
97+
longPressButton.setTitleColor(.gray, for: .normal)
98+
scrollView.addSubview(longPressButton)
99+
longPressButton.snp.makeConstraints { (make) in
100+
make.top.equalTo(button1.snp.bottom).offset(20)
101+
make.left.equalTo(20)
102+
make.right.equalTo(-20)
103+
make.height.equalTo(36)
104+
}
89105
}
90106

91107
override func viewWillAppear(_ animated: Bool) {
@@ -114,4 +130,9 @@ class ViewController: UIViewController {
114130
navigationController?.pushViewController(c, animated: true)
115131
}
116132

133+
var didLongTap: Bool = false
134+
135+
@objc func longTap(_ sender: UIGestureRecognizer) {
136+
didLongTap = true
137+
}
117138
}

Example/SpecToolsExampleTests/ViewControllerSpec.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ViewControllerSpec: QuickSpec {
2222
var subject: ViewController!
2323

2424
var button1: UIButton!
25+
var longPressButton: UIButton!
2526

2627
describe("ViewController") {
2728
beforeEach {
@@ -38,6 +39,9 @@ class ViewControllerSpec: QuickSpec {
3839

3940
// Get a button that equals "Launch table view controller" and print how we get to it in the console including all frames and any text if present
4041
button1 = subject.view.spec.find.first(buttonWithText: "Launch table view controller", visualize: .all)!
42+
43+
// Get a button that equals "Long press button"
44+
longPressButton = subject.view.spec.find.first(buttonWithText: "Long press button")!
4145
}
4246

4347
it("should have gone through the whole initial lifecycle procedure") {
@@ -104,6 +108,18 @@ class ViewControllerSpec: QuickSpec {
104108
expect(subject.navigationController?.spec.check.contains(viewControllerClass: TableViewController.self)).to(beTrue())
105109
}
106110
}
111+
112+
describe("when we long press on long press button") {
113+
beforeEach {
114+
// Simulate long press on button
115+
longPressButton.spec.action.triggerLongPress()
116+
}
117+
118+
it("should have pushed a new view controller") {
119+
// Check longTap function was called
120+
expect(subject.didLongTap).toEventually(beTrue())
121+
}
122+
}
107123
}
108124
}
109125
}

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,23 @@ collectionView.spec.action.tap(item: 3)
183183
// or
184184
collectionView.spec.action.tap(item: 2, section: 1)
185185
```
186-
186+
------
187187
#### Simulating swipes
188188

189189
Simulate swipe on a view with UISwipeGestureRecognizer(s)
190190
```Swift
191191
view.spec.action.triggerSwipe(direction: .up)
192192
```
193+
194+
------
195+
#### Simulating long presses
196+
197+
Simulate swipe on a view with UILongPressGestureRecognizer(s)
198+
```Swift
199+
button.spec.action.triggerLongPress()
200+
// or
201+
button.spec.action.triggerLongPress(minimumPressDuration: 1.0, taps: 3, touches: 2)
202+
```
193203
------
194204

195205
#### Executing gesture recognizers (not available on tvOS)

SpecTools/Classes/Action/Action+UIGestureRecognizer.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,25 @@ public typealias TargetActionInfo = [(target: AnyObject, action: Selector)]
4747

4848
return self
4949
}
50-
50+
51+
/// Simulate long press on a gesture recognizer
52+
/// - Parameter minimumPressDuration: Minimum period of time before gesture recognized
53+
/// - Parameter taps: Number of taps
54+
/// - Parameter touches: Number of touches
55+
@discardableResult public func triggerLongPress(minimumPressDuration: TimeInterval = 0.5, taps: Int = 1, touches: Int = 1) -> Action {
56+
guard element.isUserInteractionEnabled else {
57+
fatalError("User interactions are disabled. Gesture recognizer can't be used.")
58+
}
59+
let recognizers = element.spec.find.all(gestureRecognizersOfType: UILongPressGestureRecognizer.self)
60+
for recognizer in recognizers {
61+
if recognizer.minimumPressDuration <= minimumPressDuration &&
62+
recognizer.numberOfTapsRequired == taps &&
63+
recognizer.numberOfTouchesRequired == touches {
64+
recognizer.spec.action.execute()
65+
}
66+
}
67+
return self
68+
}
5169
}
5270

5371

0 commit comments

Comments
 (0)