@@ -10,43 +10,57 @@ import SPQRCode
1010
1111class ViewController : UIViewController {
1212
13- private lazy var label = UILabel ( )
13+ private lazy var titleLabel = UILabel ( )
14+ private lazy var resultLabel = UILabel ( )
1415 private lazy var stackView = UIStackView ( )
15- private lazy var simplePresent = UIButton ( configuration: . borderedTinted( ) , primaryAction: nil )
16+ private lazy var showCustomScannerButton = createCustomButton ( )
17+ private lazy var showBasicScannerButton = createBasicButton ( )
1618
1719 override func viewDidLoad( ) {
1820 super. viewDidLoad ( )
1921
2022 view. backgroundColor = . systemBackground
21- view. addSubview ( label)
23+ view. addSubview ( titleLabel)
24+ view. addSubview ( resultLabel)
2225 view. addSubview ( stackView)
2326
24- label. translatesAutoresizingMaskIntoConstraints = false
27+ titleLabel. translatesAutoresizingMaskIntoConstraints = false
28+ resultLabel. translatesAutoresizingMaskIntoConstraints = false
2529 stackView. translatesAutoresizingMaskIntoConstraints = false
2630
2731 NSLayoutConstraint . activate ( [
28- label. leadingAnchor. constraint ( equalTo: view. leadingAnchor, constant: 32 ) ,
29- label. trailingAnchor. constraint ( equalTo: view. trailingAnchor, constant: - 32 ) ,
30- label. topAnchor. constraint ( equalTo: view. safeAreaLayoutGuide. topAnchor, constant: 45 ) ,
32+ titleLabel. leadingAnchor. constraint ( equalTo: view. leadingAnchor, constant: 32 ) ,
33+ titleLabel. topAnchor. constraint ( equalTo: view. safeAreaLayoutGuide. topAnchor, constant: 45 ) ,
34+
35+ resultLabel. topAnchor. constraint ( equalTo: titleLabel. bottomAnchor, constant: 8 ) ,
36+ resultLabel. leadingAnchor. constraint ( equalTo: view. leadingAnchor, constant: 32 ) ,
37+ resultLabel. trailingAnchor. constraint ( equalTo: view. trailingAnchor, constant: - 32 ) ,
3138
3239 stackView. leadingAnchor. constraint ( equalTo: view. leadingAnchor, constant: 64 ) ,
3340 stackView. trailingAnchor. constraint ( equalTo: view. trailingAnchor, constant: - 64 ) ,
3441 stackView. bottomAnchor. constraint ( equalTo: view. safeAreaLayoutGuide. bottomAnchor, constant: - 64 ) ,
42+
43+ showCustomScannerButton. heightAnchor. constraint ( equalToConstant: 48 ) ,
44+ showBasicScannerButton. heightAnchor. constraint ( equalToConstant: 48 ) ,
3545 ] )
3646
37- stackView. addArrangedSubview ( simplePresent)
47+ stackView. spacing = 16
48+ stackView. axis = . vertical
49+ stackView. addArrangedSubview ( showCustomScannerButton)
50+ stackView. addArrangedSubview ( showBasicScannerButton)
3851
39- simplePresent . setTitle ( " Show Scanner " , for : . normal )
40- simplePresent . addTarget ( self , action : #selector ( showScanner ) , for : . touchUpInside )
52+ titleLabel . text = " Detected: "
53+ titleLabel . font = . systemFont ( ofSize : 17 , weight : . semibold )
4154
42- label. text = " Detected: \n --- "
55+ resultLabel. numberOfLines = 0
56+ resultLabel. textColor = . secondaryLabel
4357 }
4458
45- @objc private func showScanner ( ) {
59+ @objc private func showBasicScanner ( ) {
4660 let vc = SPQRCameraViewController ( )
4761
4862 vc. cameraFoundHandler = { [ weak self] value in
49- self ? . label . text = " Detected: \n " + value
63+ self ? . resultLabel . text = value
5064 }
5165 vc. cameraDidPressHandler = { [ weak vc] in
5266 vc? . dismiss ( animated: true , completion: nil )
@@ -55,5 +69,41 @@ class ViewController: UIViewController {
5569 present ( vc, animated: true , completion: nil )
5670 }
5771
72+ @objc private func showCustomScanner( ) {
73+ let vc = CustomCameraViewController ( )
74+
75+ vc. cameraFoundHandler = { [ weak self] value in
76+ self ? . resultLabel. text = value
77+ }
78+ vc. cameraDidPressHandler = { [ weak vc] in
79+ vc? . dismiss ( animated: true , completion: nil )
80+ }
81+
82+ present ( vc, animated: true , completion: nil )
83+ }
84+
85+ private func createBasicButton( ) -> UIButton {
86+ var configuration = UIButton . Configuration. borderedProminent ( )
87+ configuration. contentInsets. top = 8
88+ configuration. contentInsets. bottom = 8
89+ configuration. title = " Show Basic Scanner "
90+ configuration. cornerStyle = . dynamic
91+
92+ let button = UIButton ( configuration: configuration, primaryAction: nil )
93+ button. addTarget ( self , action: #selector( showBasicScanner) , for: . touchUpInside)
94+ return button
95+ }
96+
97+ private func createCustomButton( ) -> UIButton {
98+ var configuration = UIButton . Configuration. borderedProminent ( )
99+ configuration. contentInsets. top = 8
100+ configuration. contentInsets. bottom = 8
101+ configuration. title = " Show Custom Scanner "
102+ configuration. cornerStyle = . dynamic
103+
104+ let button = UIButton ( configuration: configuration, primaryAction: nil )
105+ button. addTarget ( self , action: #selector( showCustomScanner) , for: . touchUpInside)
106+ return button
107+ }
58108}
59109
0 commit comments