Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIPinchGestureRecognizer not getting added #1087

Open
utkarsh-bandekar opened this issue Feb 12, 2025 · 1 comment
Open

UIPinchGestureRecognizer not getting added #1087

utkarsh-bandekar opened this issue Feb 12, 2025 · 1 comment

Comments

@utkarsh-bandekar
Copy link

I want to implement pinch to zoom the camera but the adding of the gesture does not seems to be working, HandlePinch method is not getting called. following is my code. not sure what i am doing wrong.

`using System;
using System.Linq;
using AVFoundation;
using CoreGraphics;
using Foundation;
using SampleConsumer.iOS;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using ZXing.Net.Mobile.Forms;
using ZXing.Net.Mobile.Forms.iOS;

[assembly: ExportRenderer(typeof(ZXingScannerView), typeof(CustomZXingRenderer))]
namespace SampleConsumer.iOS
{
public class CustomZXingRenderer : ZXingScannerViewRenderer
{
private UIPinchGestureRecognizer pinchGesture;
private AVCaptureDevice captureDevice;

    protected override void OnElementChanged(ElementChangedEventArgs<ZXingScannerView> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            Control.UserInteractionEnabled = true;
            SetupPinchToZoom();
        }
    }
    private void SetupPinchToZoom()
    {
        try
        {
            if (Control != null) // Use 'Control' instead of 'NativeView'
            {
                pinchGesture = new UIPinchGestureRecognizer(HandlePinch);
                Control.AddGestureRecognizer(pinchGesture); // Attach to Control
                ShowDebugPopup("Info", "Pinch-to-zoom enabled.");
            }
            else
            {
                ShowDebugPopup("Error", "Control is null. Cannot add gesture recognizer.");
            }
        }
        catch (Exception ex)
        {
            ShowDebugPopup("Error", $"Failed to set up pinch zoom: {ex.Message}");
        }
    }

    private void HandlePinch(UIPinchGestureRecognizer gesture)
    {
        try
        {
            ShowDebugPopup("Debug", "HandlePinch called!"); // Debugging

            if (captureDevice == null)
            {
                var devices = AVCaptureDeviceDiscoverySession.Create(
                    new AVCaptureDeviceType[] { AVCaptureDeviceType.BuiltInWideAngleCamera },
                    AVMediaType.Video,
                    AVCaptureDevicePosition.Back)?.Devices;
                captureDevice = devices?.FirstOrDefault();
            }

            if (captureDevice != null && gesture.State == UIGestureRecognizerState.Changed)
            {
                NSError error;
                if (captureDevice.LockForConfiguration(out error))
                {
                    float minZoom = 1.0f;
                    float maxZoom = Math.Min((float)captureDevice.ActiveFormat.VideoMaxZoomFactor, 4.0f);
                    float newZoom = (float)(captureDevice.VideoZoomFactor * gesture.Scale);
                    newZoom = Math.Max(minZoom, Math.Min(newZoom, maxZoom));

                    captureDevice.VideoZoomFactor = newZoom;
                    captureDevice.UnlockForConfiguration();

                    gesture.Scale = 1.0f; // Reset scale
                    ShowDebugPopup("Zoom", $"Zoom Level: {newZoom}");
                }
            }
        }
        catch (Exception ex)
        {
            ShowDebugPopup("Error", $"Pinch handling failed: {ex.Message}");
        }
    }


    private void ShowDebugPopup(string title, string message)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            var alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
            alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));

            var window = UIApplication.SharedApplication.KeyWindow;
            var vc = window.RootViewController;

            while (vc.PresentedViewController != null)
            {
                vc = vc.PresentedViewController;
            }

            vc.PresentViewController(alert, true, null);
        });
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && pinchGesture != null)
        {
            NativeView?.RemoveGestureRecognizer(pinchGesture);
            pinchGesture.Dispose();
            pinchGesture = null;
        }
        base.Dispose(disposing);
    }
}

}
`

@utkarsh-bandekar
Copy link
Author

@Redth : Can you please help me here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant