From 0c8b5b049618eb3c15dab3e481ff55bae6b17b75 Mon Sep 17 00:00:00 2001 From: Gk40002148 Date: Thu, 28 Apr 2022 16:16:15 +0900 Subject: [PATCH] =?UTF-8?q?QR=E3=82=B3=E3=83=BC=E3=83=89=E8=AA=AD=E5=8F=96?= =?UTF-8?q?=E4=B8=AD=E3=81=AE=E7=94=BB=E9=9D=A2=E5=9B=9E=E8=BB=A2=E3=81=97?= =?UTF-8?q?=E3=81=9F=E9=9A=9B=E3=81=AB=E3=83=AC=E3=82=A4=E3=82=A2=E3=82=A6?= =?UTF-8?q?=E3=83=88=E3=81=AE=E5=86=8D=E8=AA=BF=E6=95=B4=E3=82=92=E8=A1=8C?= =?UTF-8?q?=E3=81=86=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Login/View/LoginViewController.swift | 11 +++- AgileWorks/Common/QRCodeReader.swift | 50 +++++++++++++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/AgileWorks/AgileWorks/Login/View/LoginViewController.swift b/AgileWorks/AgileWorks/Login/View/LoginViewController.swift index efbfca4..60276be 100644 --- a/AgileWorks/AgileWorks/Login/View/LoginViewController.swift +++ b/AgileWorks/AgileWorks/Login/View/LoginViewController.swift @@ -118,6 +118,16 @@ class LoginViewController: UIViewController { contextTextField.frame.origin.x = slashLabel.frame.origin.x + slashWidth } + override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { + super.viewWillTransition(to: size, with: coordinator) + + coordinator.animate(alongsideTransition: nil, completion: {_ in + if self.qrCodeReader.delegate != nil { + self.qrCodeReader.previewLayerTransition(self.view) + } + }) + } + override func touchesBegan(_ touches: Set, with event: UIEvent?) { self.view.endEditing(true) } @@ -130,7 +140,6 @@ class LoginViewController: UIViewController { //サーバー名コンテキストパスの保存 KeychainDataStore().writeServerURL(serverURL: serverHost) KeychainDataStore().writeContextPath(contextPath: context) - presenter.fetch { result in switch result { case .success: diff --git a/AgileWorks/Common/QRCodeReader.swift b/AgileWorks/Common/QRCodeReader.swift index 00f31c4..e66c853 100644 --- a/AgileWorks/Common/QRCodeReader.swift +++ b/AgileWorks/Common/QRCodeReader.swift @@ -78,8 +78,22 @@ public class QRCodeReader { previewLayer.frame = view.bounds //previewLayer.frame = CGRect(x: 0, y: 90, width: view.frame.width, height: view.frame.height - 90) previewLayer.videoGravity = .resizeAspectFill + self.setVideoOrientation() view.layer.addSublayer(previewLayer) } + // pareviewLayter 再設定 + public func previewLayerTransition(_ view: UIView) { + self.previewLayer.frame = view.bounds + self.setReadRangeFrame() + self.setGuideFrame() + self.setVideoOrientation() + } + // 画面向きをセット + private func setVideoOrientation() { + if let orientation = self.convertUIOrientation2VideoOrientation(function: { return self.appOrientation() }) { + self.previewLayer.connection?.videoOrientation = orientation + } + } // 認識QRコード確認表示設定 private func targetCapture(borderWidth: Int, borderColor: CGColor) { @@ -98,10 +112,16 @@ public class QRCodeReader { self.rangeView.layer.borderWidth = 1 self.rangeView.layer.borderColor = UIColor.red.cgColor if let preview = self.preview { - self.rangeView.frame = CGRect(x: preview.frame.size.width * frame.minX, y: preview.frame.size.height * frame.minY, width: preview.frame.size.width * frame.size.width, height: preview.frame.size.height * frame.size.height) + self.setReadRangeFrame() preview.addSubview(rangeView) } } + // 読取範囲のフレーム設定 + private func setReadRangeFrame(frame: CGRect = CGRect(x: 0.2, y: 0.3, width: 0.6, height: 0.4)) { + if let preview = self.preview { + self.rangeView.frame = CGRect(x: preview.frame.size.width * frame.minX, y: preview.frame.size.height * frame.minY, width: preview.frame.size.width * frame.size.width, height: preview.frame.size.height * frame.size.height) + } + } // キャンセルボタン生成 private func cancelButton() { @@ -122,14 +142,19 @@ public class QRCodeReader { public func setCameraGuide(guide: String) { self.cameraGuideLbl = UILabel() if let preview = self.preview { - self.cameraGuideLbl.frame = CGRect(x: 0, y: preview.frame.height - 60, width: preview.frame.width, height: 30) + self.setGuideFrame() self.cameraGuideLbl.text = guide self.cameraGuideLbl.textColor = .white self.cameraGuideLbl.textAlignment = NSTextAlignment.center preview.addSubview(cameraGuideLbl) } } - + // ガイダンスフレーム設定 + private func setGuideFrame() { + if let preview = self.preview { + self.cameraGuideLbl.frame = CGRect(x: 0, y: preview.frame.height - 60, width: preview.frame.width, height: 30) + } + } // カメラ停止 public func stopRunning() { if self.captureSession == nil { return } @@ -171,4 +196,23 @@ public class QRCodeReader { func cancelBtnTaped(_ sender: UIButton) { self.stopRunning() } + + private func appOrientation() -> UIInterfaceOrientation { + return UIApplication.shared.statusBarOrientation + } + // UIInterfaceOrientation -> AVCaptureVideoOrientation に convert + private func convertUIOrientation2VideoOrientation(function: () -> UIInterfaceOrientation) -> AVCaptureVideoOrientation? { + let val = function() + switch val { + case UIInterfaceOrientation.unknown: + return nil + default: + return ([ + UIInterfaceOrientation.portrait: AVCaptureVideoOrientation.portrait, + UIInterfaceOrientation.portraitUpsideDown: AVCaptureVideoOrientation.portraitUpsideDown, + UIInterfaceOrientation.landscapeLeft: AVCaptureVideoOrientation.landscapeLeft, + UIInterfaceOrientation.landscapeRight: AVCaptureVideoOrientation.landscapeRight + ])[val] + } + } } -- GitLab