Skip to content

Commit aa1aca7

Browse files
Update to initial version
1 parent b13a21b commit aa1aca7

4 files changed

Lines changed: 60 additions & 27 deletions

File tree

Sources/SPQRCode/Extensions/UIColor+Extensions.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1+
// The MIT License (MIT)
2+
// Copyright © 2020 Ivan Vorobei (hello@ivanvorobei.io)
13
//
2-
// File.swift
3-
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
410
//
5-
// Created by Nikita Somenkov on 07.03.2022.
11+
// The above copyright notice and this permission notice shall be included in all
12+
// copies or substantial portions of the Software.
613
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
721

822
import Foundation
923
import UIKit

Sources/SPQRCode/SPQRCameraViewController.swift

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@ import AVFoundation
2525
/**
2626
SPQRCameraViewController: Main view. Can be customisable if need.
2727

28-
For change duration, check method `present` and pass duration and other specific property if need customise.
28+
Use `delegate` or `cameraFoundHandler` callback to get recognition results.
2929

30-
Here available set window on which shoud be present.
31-
If you have some windows, you shoud configure it. Check property `presentWindow`.
30+
Example:
3231

33-
For disable dismiss by tap, check property `.dismissByTap`.
34-
35-
Recomended call `SPAlert` and choose style func.
32+
let viewController = SPQRCameraViewController()
33+
otherViewController.present(viewController)
3634
*/
3735
open class SPQRCameraViewController: UIViewController {
3836

@@ -57,8 +55,8 @@ open class SPQRCameraViewController: UIViewController {
5755
public var cameraFoundHandler: SPQRCameraFoundHandler?
5856
public var cameraDidPressHandler: SPQRCameraHandlerDidPressHandler?
5957

60-
private lazy var frameView: UIView = SPFrameView()
61-
private lazy var previewView: UIView = SPPreviewLabel()
58+
public var customFrameView: UIView?
59+
public var customPreviewView: UIView?
6260

6361
// MARK: - Private Properties
6462

@@ -67,6 +65,15 @@ open class SPQRCameraViewController: UIViewController {
6765
// MARK: - Sublayers
6866

6967
private lazy var videoPreviewLayer: AVCaptureVideoPreviewLayer = createVideoPreviewLayer()
68+
private lazy var defaultFrameView = SPFrameView()
69+
private lazy var defaultPreviewView = SPPreviewLabel()
70+
71+
private var frameView: UIView {
72+
customFrameView ?? defaultFrameView
73+
}
74+
private var previewView: UIView {
75+
customPreviewView ?? defaultPreviewView
76+
}
7077

7178
// MARK: - Private Properties
7279

@@ -82,7 +89,7 @@ open class SPQRCameraViewController: UIViewController {
8289
super.init(coder: coder)
8390
}
8491

85-
public override func viewDidLoad() {
92+
open override func viewDidLoad() {
8693
super.viewDidLoad()
8794
configureView()
8895
configureActions()
@@ -91,20 +98,24 @@ open class SPQRCameraViewController: UIViewController {
9198
captureSession.startRunning()
9299
}
93100

94-
public override func viewWillLayoutSubviews() {
101+
open override func viewWillLayoutSubviews() {
95102
super.viewWillLayoutSubviews()
96103
videoPreviewLayer.frame = view.layer.bounds
97104
}
98105

99106
open func updatePreviewView(for object: AVMetadataMachineReadableCodeObject) {
100-
if let previewView = previewView as? SPPreviewLabel {
101-
previewView.text = object.stringValue
107+
if let string = object.stringValue {
108+
if let url = URL(string: string) {
109+
defaultPreviewView.text = "URL: \"\(url.absoluteString)\""
110+
} else {
111+
defaultPreviewView.text = "Text: \"\(string)\""
112+
}
102113
}
103114
}
104115

105116
// MARK: - Actions
106117

107-
@objc private func previewDidPress() {
118+
@objc func previewDidPress(_ sender: UITapGestureRecognizer) {
108119
notifyDidPress()
109120
}
110121

@@ -121,7 +132,6 @@ private extension SPQRCameraViewController {
121132
}
122133

123134
private func configureSubviews() {
124-
frameView.backgroundColor = .clear
125135
frameView.isHidden = true
126136
previewView.isHidden = true
127137
}
@@ -165,11 +175,17 @@ private extension SPQRCameraViewController {
165175
}
166176

167177
private func configureActions() {
168-
previewView.isUserInteractionEnabled = true
169-
frameView.isUserInteractionEnabled = true
178+
let previewViewTapGesture = UITapGestureRecognizer(
179+
target: self,
180+
action: #selector(previewDidPress)
181+
)
182+
let frameViewTapGesture = UITapGestureRecognizer(
183+
target: self,
184+
action: #selector(previewDidPress)
185+
)
170186

171-
previewView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(previewDidPress)))
172-
frameView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(previewDidPress)))
187+
previewView.addGestureRecognizer(previewViewTapGesture)
188+
frameView.addGestureRecognizer(frameViewTapGesture)
173189
}
174190

175191
}
@@ -179,8 +195,8 @@ private extension SPQRCameraViewController {
179195
extension SPQRCameraViewController: AVCaptureMetadataOutputObjectsDelegate {
180196

181197
open func metadataOutput(_ output: AVCaptureMetadataOutput,
182-
didOutput metadataObjects: [AVMetadataObject],
183-
from connection: AVCaptureConnection) {
198+
didOutput metadataObjects: [AVMetadataObject],
199+
from connection: AVCaptureConnection) {
184200
guard !metadataObjects.isEmpty else {
185201
return
186202
}
@@ -207,10 +223,10 @@ extension SPQRCameraViewController: AVCaptureMetadataOutputObjectsDelegate {
207223
if frameView.isHidden {
208224
frameView.frame = frame
209225
} else {
210-
UIView.animate(withDuration: 0.3) {
226+
UIView.animate(withDuration: 0.3, delay: 0.0, options: .allowUserInteraction, animations: {
211227
self.frameView.frame = frame
212228
self.view.layoutIfNeeded()
213-
}
229+
}, completion: nil)
214230
}
215231

216232
previewView.isHidden = false
@@ -253,7 +269,7 @@ private extension SPQRCameraViewController {
253269

254270
private func createVideoPreviewLayer() -> AVCaptureVideoPreviewLayer {
255271
let videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
256-
videoPreviewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
272+
videoPreviewLayer.videoGravity = .resizeAspectFill
257273
return videoPreviewLayer
258274
}
259275

Sources/SPQRCode/Views/SPFrameView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ private extension SPFrameView {
102102
shapeLayer.strokeColor = UIColor.frameColor.cgColor
103103
shapeLayer.fillColor = nil
104104
shapeLayer.lineWidth = lineWidth
105+
106+
backgroundColor = .clear
105107
}
106108

107109
}

Sources/SPQRCode/Views/SPPreviewLabel.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public class SPPreviewLabel: UILabel {
5959
private extension SPPreviewLabel {
6060

6161
private func comminInit() {
62-
font = .preferredFont(forTextStyle: .caption1)
62+
font = .systemFont(ofSize: 15, weight: .medium)
63+
isUserInteractionEnabled = true
6364
clipsToBounds = true
6465
backgroundColor = .frameColor
6566
textColor = .black

0 commit comments

Comments
 (0)