Skip to content

UIPinchGestureRecognizer not getting added #1087

@utkarsh-bandekar

Description

@utkarsh-bandekar

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);
    }
}

}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions