forked from RxSwiftCommunity/RxGesture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.swift
403 lines (345 loc) · 11.7 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//
// ViewController.swift
// RxGesture-OSX
//
// Created by Marin Todorov on 3/24/16.
// Copyright © 2016 CocoaPods. All rights reserved.
//
import Cocoa
import RxSwift
import RxCocoa
import RxGesture
class Step {
enum Action { case previous, next }
let title: String
let code: String
let install: (NSView, NSTextField, AnyObserver<Action>, DisposeBag) -> Void
init(title: String, code: String, install: @escaping (NSView, NSTextField, AnyObserver<Action>, DisposeBag) -> Void) {
self.title = title
self.code = code
self.install = install
}
}
class MacViewController: NSViewController {
@IBOutlet private weak var myView: NSView!
@IBOutlet private weak var myViewText: NSTextField!
@IBOutlet private weak var info: NSTextField!
@IBOutlet private weak var code: NSTextView!
fileprivate let nextStepObserver = PublishSubject<Step.Action>()
fileprivate let bag = DisposeBag()
fileprivate var stepBag = DisposeBag()
override func viewWillAppear() {
super.viewWillAppear()
view.wantsLayer = true
myView.wantsLayer = true
myView.layer?.backgroundColor = NSColor.red.cgColor
myView.layer?.cornerRadius = 5
let steps: [Step] = [
clickStep,
doubleClickStep,
rightClickStep,
anyClickStep,
pressStep,
panStep,
rotateStep,
magnificationStep
]
func newIndex(for index: Int, action: Step.Action) -> Int {
switch action {
case .previous:
return index > 0 ? index - 1 : steps.count - 1
case .next:
return index < steps.count - 1 ? index + 1 : 0
}
}
nextStepObserver
.scan(0, accumulator: newIndex)
.startWith(0)
.map { (steps[$0], $0) }
.subscribe(onNext: { [unowned self] in self.updateStep($0, at: $1) })
.disposed(by: bag)
}
override func viewDidAppear() {
super.viewDidAppear()
updateAnchorPoint()
}
private func updateAnchorPoint() {
let frame = myView.layer!.frame
myView.layer!.anchorPoint = CGPoint(x: 0.5, y: 0.5)
myView.layer!.frame = frame
}
@IBAction func previousStep(_ sender: Any) {
nextStepObserver.onNext(.previous)
}
@IBAction func nextStep(_ sender: Any) {
nextStepObserver.onNext(.next)
}
func updateStep(_ step: Step, at index: Int) {
stepBag = DisposeBag()
info.stringValue = "\(index + 1). " + step.title
code.string = step.code
myViewText.stringValue = ""
step.install(myView, myViewText, nextStepObserver.asObserver(), stepBag)
print("active gestures: \(myView.gestureRecognizers.count)")
}
lazy var clickStep: Step = Step(
title: "Click the square",
code: """
view.rx
.leftClickGesture()
.when(.recognized)
.subscribe(onNext: { _ in
// Do something
})
.disposed(by: disposeBag)
""",
install: { view, _, nextStep, stepBag in
view.animateTransform(to: CATransform3DIdentity)
view.animateBackgroundColor(to: .red)
view.rx
.leftClickGesture()
.when(.recognized)
.subscribe(onNext: { _ in
nextStep.onNext(.next)
})
.disposed(by: stepBag)
})
lazy var doubleClickStep: Step = Step(
title: "Double click the square",
code: """
view.rx
.leftClickGesture { gesture, _ in
gesture.numberOfClicksRequired = 2
}
.when(.recognized)
.subscribe(onNext: { _ in
// Do something
})
.disposed(by: disposeBag)
""",
install: { view, _, nextStep, stepBag in
view.animateTransform(to: CATransform3DIdentity)
view.animateBackgroundColor(to: .green)
view.rx
.leftClickGesture { gesture, _ in
gesture.numberOfClicksRequired = 2
}
.when(.recognized)
.subscribe(onNext: { _ in
nextStep.onNext(.next)
})
.disposed(by: stepBag)
})
lazy var rightClickStep: Step = Step(
title: "Right click the square",
code: """
view.rx
.rightClickGesture()
.when(.recognized)
.subscribe(onNext: { _ in
// Do something
})
.disposed(by: disposeBag)
""",
install: { view, _, nextStep, stepBag in
view.animateTransform(to: CATransform3DIdentity)
view.animateBackgroundColor(to: .blue)
view.rx
.rightClickGesture()
.when(.recognized)
.subscribe(onNext: { _ in
nextStep.onNext(.next)
})
.disposed(by: stepBag)
})
lazy var anyClickStep: Step = Step(
title: "Click any button (left or right)",
code: """
view.rx
.anyGesture(.leftClick(), .rightClick())
.when(.recognized)
.subscribe(onNext: { _ in
// Do something
})
.disposed(by: disposeBag)
""",
install: { view, _, nextStep, stepBag in
view.animateTransform(to: CATransform3DMakeScale(1.5, 1.5, 1.0))
view.animateBackgroundColor(to: .red)
view.rx
.anyGesture(.leftClick(), .rightClick())
.when(.recognized)
.subscribe(onNext: { _ in
nextStep.onNext(.next)
})
.disposed(by: stepBag)
})
lazy var pressStep: Step = Step(
title: "Long press the square",
code: """
view.rx
.pressGesture()
.when(.began)
.subscribe(onNext: { _ in
// Do something
})
.disposed(by: disposeBag)
""",
install: { view, _, nextStep, stepBag in
view.animateBackgroundColor(to: .red)
view.animateTransform(to: CATransform3DMakeScale(2.0, 2.0, 1.0))
view.rx
.pressGesture()
.when(.began)
.subscribe(onNext: { _ in
nextStep.onNext(.next)
})
.disposed(by: stepBag)
})
lazy var panStep: Step = Step(
title: "Drag the square around",
code: """
view.rx
.panGesture()
.when(.changed)
.asTranslation()
.subscribe(onNext: { _ in
// Do something
})
.disposed(by: disposeBag)
view.rx
.anyGesture(
(.pan(), when: .ended),
(.click(), when: .recognized)
)
.subscribe(onNext: { _ in
// Do something
})
.disposed(by: disposeBag)
""",
install: { view, label, nextStep, stepBag in
view.animateTransform(to: CATransform3DIdentity)
view.rx
.panGesture()
.when(.changed)
.asTranslation()
.subscribe(onNext: { translation, _ in
label.stringValue = String(format: "(%.f, %.f)", arguments: [translation.x, translation.y])
view.layer!.transform = CATransform3DMakeTranslation(translation.x, translation.y, 0.0)
})
.disposed(by: stepBag)
view.rx
.anyGesture(
(.pan(), when: .ended),
(.leftClick(), when: .recognized)
)
.subscribe(onNext: { _ in
nextStep.onNext(.next)
})
.disposed(by: stepBag)
})
lazy var rotateStep: Step = Step(
title: "Rotate the square with your trackpad, or click if you do not have a trackpad",
code: """
view.rx
.rotationGesture()
.when(.changed)
.asRotation()
.subscribe(onNext: { _ in
// Do something
})
.disposed(by: disposeBag)
view.rx
.anyGesture(
(.rotation(), when: .ended),
(.click(), when: .recognized)
)
.subscribe(onNext: { _ in
// Do something
})
.disposed(by: disposeBag)
""",
install: { view, label, nextStep, stepBag in
view.animateTransform(to: CATransform3DIdentity)
view.rx
.rotationGesture()
.when(.changed)
.asRotation()
.subscribe(onNext: { rotation in
label.stringValue = String(format: "angle: %.2f", rotation)
view.layer!.transform = CATransform3DMakeRotation(rotation, 0, 0, 1)
})
.disposed(by: stepBag)
view.rx
.anyGesture(
(.rotation(), when: .ended),
(.leftClick(), when: .recognized)
)
.subscribe(onNext: { _ in
nextStep.onNext(.next)
})
.disposed(by: stepBag)
})
lazy var magnificationStep: Step = Step(
title: "Pinch the square with your trackpad, or click if you do not have a trackpad",
code: """
view.rx
.magnificationGesture()
.when(.changed)
.asScale()
.subscribe(onNext: { _ in
// Do something
})
.disposed(by: disposeBag)
view.rx
.anyGesture(
(.magnification(), when: .ended),
(.click(), when: .recognized)
)
.subscribe(onNext: { _ in
// Do something
})
.disposed(by: disposeBag)
""",
install: { view, label, nextStep, stepBag in
view.animateTransform(to: CATransform3DIdentity)
view.rx
.magnificationGesture()
.when(.changed)
.asScale()
.subscribe(onNext: { scale in
label.stringValue = String(format: "scale: %.2f", scale)
view.layer!.transform = CATransform3DMakeScale(scale, scale, 1)
})
.disposed(by: stepBag)
view.rx
.anyGesture(
(.magnification(), when: .ended),
(.leftClick(), when: .recognized)
)
.subscribe(onNext: { _ in
nextStep.onNext(.next)
})
.disposed(by: stepBag)
})
}
private extension NSView {
func animateTransform(to transform: CATransform3D) {
let initialTransform = self.layer!.transform
let anim = CABasicAnimation(keyPath: "transform")
anim.duration = 0.5
anim.fromValue = NSValue(caTransform3D: initialTransform)
anim.toValue = NSValue(caTransform3D: transform)
self.layer!.add(anim, forKey: nil)
self.layer!.transform = transform
}
func animateBackgroundColor(to color: NSColor) {
let initialColor = self.layer!.backgroundColor!
let anim = CABasicAnimation(keyPath: "backgroundColor")
anim.duration = 0.5
anim.fromValue = initialColor
anim.toValue = color.cgColor
self.layer!.add(anim, forKey: nil)
self.layer!.backgroundColor = color.cgColor
}
}