Skip to content

Commit

Permalink
Update ios orientation handling
Browse files Browse the repository at this point in the history
Update ios orientation to follow application orientation and update when device orientation changes
  • Loading branch information
bstadick authored May 8, 2024
1 parent ea0f293 commit 7b29fa1
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal partial class CameraManager
AVCaptureVideoPreviewLayer videoPreviewLayer;
CaptureDelegate captureDelegate;
DispatchQueue dispatchQueue;
NSObject orientationObserver;
Dictionary<NSString, MSize> Resolutions => new()
{
{ AVCaptureSession.Preset352x288, new MSize(352, 288) },
Expand All @@ -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();
Expand Down Expand Up @@ -161,6 +173,13 @@ public void Disconnect()
captureDevice.Dispose();
captureDevice = null;
}

// Removing the orientationObserver
if (orientationObserver != null)
{
NSNotificationCenter.DefaultCenter.RemoveObserver(orientationObserver);
orientationObserver = null;
}
}
}

Expand All @@ -176,7 +195,7 @@ public void UpdateTorch(bool on)
{
CaptureDevicePerformWithLockedConfiguration(() =>
captureDevice.TorchMode = on ? AVCaptureTorchMode.On : AVCaptureTorchMode.Off);
}
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -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

0 comments on commit 7b29fa1

Please sign in to comment.