-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathCustomUIViewController.swift
64 lines (54 loc) · 1.86 KB
/
CustomUIViewController.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
//
// CustomUIViewController.swift
// YesWeScanExampleApp
//
// Created by Xaver Lohmüller on 02.05.19.
// Copyright © 2019 adorsys GmbH & Co KG. All rights reserved.
//
import YesWeScan
final class CustomUIViewController: UIViewController {
private lazy var scanner: DocumentScanner = AVDocumentScanner(delegate: self)
private var detectionLayer: CAShapeLayer = {
let layer = CAShapeLayer()
layer.fillColor = UIColor.red.withAlphaComponent(0.3).cgColor
layer.strokeColor = UIColor.red.withAlphaComponent(0.9).cgColor
layer.lineWidth = 2
layer.contentsGravity = .resizeAspectFill
return layer
}()
override func viewDidLoad() {
super.viewDidLoad()
scanner.previewLayer.frame = UIScreen.main.bounds
view.layer.addSublayer(scanner.previewLayer)
scanner.previewLayer.addSublayer(detectionLayer)
}
}
extension CustomUIViewController: DocumentScannerDelegate {
func didCapture(image: UIImage) {
let imageView = UIImageView(image: image)
imageView.showAnimated(on: view)
scanner.stop()
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
imageView.hideAnimated()
self.scanner.start()
}
}
func didRecognize(feature: RectangleFeature?, in image: CIImage) {
detectionLayer.path = feature?.bezierPath.cgPath
}
}
private extension UIView {
func showAnimated(on view: UIView, duration: TimeInterval = 0.5) {
frame = view.frame
alpha = 0
view.addSubview(self)
UIView.animate(withDuration: duration) {
self.alpha = 1
}
}
func hideAnimated(duration: TimeInterval = 0.5) {
UIView.animate(withDuration: duration,
animations: { self.alpha = 0 },
completion: { _ in self.removeFromSuperview() })
}
}