From 7b29fa16d581c7b8fee05bd827abc9a3c0d065e5 Mon Sep 17 00:00:00 2001 From: Bryan Stadick <2057235+bstadick@users.noreply.github.com> Date: Wed, 8 May 2024 15:12:45 -0500 Subject: [PATCH] Update ios orientation handling Update ios orientation to follow application orientation and update when device orientation changes --- .../Apple/CameraManager.ios.maccatalyst.cs | 46 +++++++++++++++---- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs b/ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs index 89ca9ff..73ad4e1 100644 --- a/ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs +++ b/ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs @@ -23,6 +23,7 @@ internal partial class CameraManager AVCaptureVideoPreviewLayer videoPreviewLayer; CaptureDelegate captureDelegate; DispatchQueue dispatchQueue; + NSObject orientationObserver; Dictionary Resolutions => new() { { AVCaptureSession.Preset352x288, new MSize(352, 288) }, @@ -43,11 +44,22 @@ public NativePlatformCameraPreviewView CreateNativeView() videoPreviewLayer = new AVCaptureVideoPreviewLayer(captureSession); videoPreviewLayer.VideoGravity = AVLayerVideoGravity.ResizeAspectFill; + // Observe device orientation changes to reapply layout subview + orientationObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIDevice.OrientationDidChangeNotification, OrientationChanged); + view = new PreviewView(videoPreviewLayer); return view; } + private void OrientationChanged(NSNotification notification) + { + if (view != null) + { + view.LayoutSubviews(); + } + } + public void Connect() { UpdateCamera(); @@ -161,6 +173,13 @@ public void Disconnect() captureDevice.Dispose(); captureDevice = null; } + + // Removing the orientationObserver + if (orientationObserver != null) + { + NSNotificationCenter.DefaultCenter.RemoveObserver(orientationObserver); + orientationObserver = null; + } } } @@ -176,7 +195,7 @@ public void UpdateTorch(bool on) { CaptureDevicePerformWithLockedConfiguration(() => captureDevice.TorchMode = on ? AVCaptureTorchMode.On : AVCaptureTorchMode.Off); - } + } } catch (Exception ex) { @@ -258,26 +277,33 @@ public PreviewView(AVCaptureVideoPreviewLayer layer) : base() public override void LayoutSubviews() { base.LayoutSubviews(); + + // Determine the UIInterfaceOrientation based on the current interface (application) orientation instead of the device orientation + UIInterfaceOrientation uiInterfaceOrientation = (UIInterfaceOrientation)UIApplication.SharedApplication.ConnectedScenes.ToArray() + .Select(x => x as UIWindowScene) + .LastOrDefault(x => x?.Windows != null && x.Windows.Any(y => y.IsKeyWindow))?.InterfaceOrientation; + CATransform3D transform = CATransform3D.MakeRotation(0, 0, 0, 1.0f); - switch (UIDevice.CurrentDevice.Orientation) + + switch (uiInterfaceOrientation) { - case UIDeviceOrientation.Portrait: + case UIInterfaceOrientation.Portrait: transform = CATransform3D.MakeRotation(0, 0, 0, 1.0f); break; - case UIDeviceOrientation.PortraitUpsideDown: - transform = CATransform3D.MakeRotation((nfloat)Math.PI, 0, 0, 1.0f); + case UIInterfaceOrientation.PortraitUpsideDown: + transform = CATransform3D.MakeRotation((nfloat)(Math.PI), 0, 0, 1.0f); break; - case UIDeviceOrientation.LandscapeLeft: - transform = CATransform3D.MakeRotation((nfloat)(-Math.PI / 2), 0, 0, 1.0f); + case UIInterfaceOrientation.LandscapeLeft: + transform = CATransform3D.MakeRotation((nfloat)(Math.PI / 2), 0, 0, 1.0f); break; - case UIDeviceOrientation.LandscapeRight: - transform = CATransform3D.MakeRotation((nfloat)Math.PI / 2, 0, 0, 1.0f); + case UIInterfaceOrientation.LandscapeRight: + transform = CATransform3D.MakeRotation((nfloat)(-Math.PI / 2), 0, 0, 1.0f); break; } PreviewLayer.Transform = transform; PreviewLayer.Frame = Layer.Bounds; } - } + } } #endif