From 02d57622345d961a4b3f64a9589c205044181505 Mon Sep 17 00:00:00 2001 From: Thorsten Date: Sun, 8 Nov 2020 18:11:30 +0100 Subject: [PATCH 1/4] focusImageView: Use the frame of the QRScannerView instead of the mainscreen bounds --- QRScanner/QRScannerView.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/QRScanner/QRScannerView.swift b/QRScanner/QRScannerView.swift index 4d513c8..80e6a6f 100644 --- a/QRScanner/QRScannerView.swift +++ b/QRScanner/QRScannerView.swift @@ -246,9 +246,9 @@ public class QRScannerView: UIView { } private func setupImageViews() { - let width = UIScreen.main.bounds.width * 0.618 - let x = UIScreen.main.bounds.width * 0.191 - let y = UIScreen.main.bounds.height * 0.191 + let width = self.bounds.width * 0.618 + let x = self.bounds.width * 0.191 + let y = self.bounds.height * 0.191 focusImageView = UIImageView(frame: CGRect(x: x, y: y, width: width, height: width)) focusImageView.image = focusImage ?? UIImage(named: "scan_qr_focus", in: Bundle(for: QRScannerView.self), compatibleWith: nil) addSubview(focusImageView) From 73bfd54ef9334e79985ff577605625fbcd876a40 Mon Sep 17 00:00:00 2001 From: Thorsten Date: Sun, 22 Nov 2020 12:22:54 +0100 Subject: [PATCH 2/4] Support for device orientation changes --- QRScanner/QRScannerView.swift | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/QRScanner/QRScannerView.swift b/QRScanner/QRScannerView.swift index 80e6a6f..fb2122c 100644 --- a/QRScanner/QRScannerView.swift +++ b/QRScanner/QRScannerView.swift @@ -79,6 +79,8 @@ public class QRScannerView: UIView { addPreviewLayer() setupBlurEffectView() setupImageViews() + + NotificationCenter.default.addObserver(self, selector: #selector(updatePreviewLayerForDeviceOrientation), name: UIDevice.orientationDidChangeNotification, object: nil) } public func startRunning() { @@ -134,6 +136,7 @@ public class QRScannerView: UIView { session.outputs.forEach { session.removeOutput($0) } removePreviewLayer() torchActiveObservation = nil + NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil) } // MARK: - Private @@ -265,6 +268,23 @@ public class QRScannerView: UIView { layer.addSublayer(previewLayer) self.previewLayer = previewLayer + + updatePreviewLayerForDeviceOrientation() + } + + @objc private func updatePreviewLayerForDeviceOrientation() { + var videoOrientation: AVCaptureVideoOrientation + switch UIDevice.current.orientation { + case .landscapeLeft: + videoOrientation = .landscapeRight + case .landscapeRight: + videoOrientation = .landscapeLeft + case .portraitUpsideDown: + videoOrientation = .portraitUpsideDown + default: + videoOrientation = .portrait + } + self.previewLayer?.connection?.videoOrientation = videoOrientation } private func removePreviewLayer() { From 046ae9ce4904e28e2dd188336ba8537bf4bbf17e Mon Sep 17 00:00:00 2001 From: Thorsten Date: Sun, 22 Nov 2020 12:36:10 +0100 Subject: [PATCH 3/4] focusView adaptive to landscape mode --- QRScanner/QRScannerView.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/QRScanner/QRScannerView.swift b/QRScanner/QRScannerView.swift index fb2122c..f24c04d 100644 --- a/QRScanner/QRScannerView.swift +++ b/QRScanner/QRScannerView.swift @@ -249,8 +249,8 @@ public class QRScannerView: UIView { } private func setupImageViews() { - let width = self.bounds.width * 0.618 - let x = self.bounds.width * 0.191 + let width = min(self.bounds.width, self.bounds.height) * 0.618 + let x = (self.bounds.width > self.bounds.height) ? (self.bounds.width - width) / 2 : self.bounds.width * 0.191 let y = self.bounds.height * 0.191 focusImageView = UIImageView(frame: CGRect(x: x, y: y, width: width, height: width)) focusImageView.image = focusImage ?? UIImage(named: "scan_qr_focus", in: Bundle(for: QRScannerView.self), compatibleWith: nil) From 2adf699f7a2c5f259ae5315813c95b25c0d42399 Mon Sep 17 00:00:00 2001 From: Thorsten Date: Sun, 22 Nov 2020 12:40:40 +0100 Subject: [PATCH 4/4] Resize subviews on frame change --- QRScanner/QRScannerView.swift | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/QRScanner/QRScannerView.swift b/QRScanner/QRScannerView.swift index f24c04d..4ec6995 100644 --- a/QRScanner/QRScannerView.swift +++ b/QRScanner/QRScannerView.swift @@ -128,6 +128,19 @@ public class QRScannerView: UIView { videoDevice.unlockForConfiguration() } + public override var frame: CGRect { + willSet { + } + didSet { + if oldValue.size.width != frame.size.width || oldValue.size.height != frame.size.height { + focusImageView.removeFromSuperview() + qrCodeImageView.removeFromSuperview() + setupImageViews() + self.previewLayer?.frame = self.bounds + } + } + } + deinit { setTorchActive(isOn: false) focusImageView.removeFromSuperview()