diff --git a/AgileWorks/AgileWorks/Login/View/LoginViewController.swift b/AgileWorks/AgileWorks/Login/View/LoginViewController.swift index efbfca4334ba141a3beda6c94fc27a58f1f0caed..60276be4635e1e928486f19ab2969424b9741f77 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 00f31c42af6a6f9a531f2753a2e97d997202a883..e66c8539443bd3fecb8a4ec979c52f333bd186c6 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] + } + } }