Xamarin.forms Pinch and Zoom

Is there a way to have a control that allows me to pinch and scale using controls ONLY Xamarin.Forms.

I want to display the image in any control from xamarin.forms (WebView or Image or any other) and be able to scale from the application.

+4
source share
3 answers

In the end, I used the meta viewport to scale as follows. It may not be a solution for everyone, but it worked for me.

Put the hex -64 image inside the image tag below, and then put all the html in the WebView.

this.WebView.Source = new HtmlWebViewSource { BaseUrl = URL, Html = html };

The html will be defined here.
<!DOCTYPE html>
<html lang="en-us">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=0.25, maximum-scale=3.0 user-scalable=1">
    <title></title>
    <style>
        body {
            margin: 0 20px 0 0;
            font-family: HelveticaNeue-Light, HelveticaNeue-UltraLight, Helvetica, Consolas, 'Courier New';
        }

        table {
            width: 100%;
            border: 1px outset;
            border-color: #3c4142;
        }

        td {
            font-size: 8px;
        }
    </style>
</head>
<body>
    <img src="data:image/png;base64,YourBase64imagestringhere" style="width:100%" />
</body>
</html>
+1
source

/ Forms . , .

, , , Xamarin.Forms.ContentView - PanGestureContainer, , min/max .

iOS , .

, attachable (aka Behavior), , , , PanGestureRecognizer, .

, , Xamarin .


: , , , , /. , , , , , , , .

  public abstract class BaseInteractiveGestureRecognizer : BindableObject, IInteractiveGestureRecognizer
    {
        public static readonly BindableProperty CommandProperty = BindableProperty.Create<BaseInteractiveGestureRecognizer, ICommand> ((b) => b.Command, null, BindingMode.OneWay, null, null, null, null);

        public ICommand Command {
            get {
                return (ICommand)base.GetValue (BaseInteractiveGestureRecognizer.CommandProperty);
            }
            set {
                base.SetValue (BaseInteractiveGestureRecognizer.CommandProperty, value);
            }
        }

        public object CommandParameter {get;set;} // make bindable as above

        public GestureState State { get;set;} // make bindable as above

        public View SourceView{ get; set; } 

        public void Send ()
        { 
            if (Command != null && Command.CanExecute (this)) {
                Command.Execute (this);
            }
        }
    }

public class PanGesture : BaseInteractiveGestureRecognizer
{
    public uint MinTouches { get;set; } // make bindable
    public uint MaxTouches { get;set; } // make bindable
    // add whatever other properties you need here - starting point, end point, touch count, current touch points etc.
}

iOS:

public abstract class BaseInteractiveGestureRenderer : BindableObject,IGestureCreator<UIView>
    {
        public abstract object Create (IInteractiveGestureRecognizer gesture, Element formsView, UIView nativeView);

        public static GestureState FromUIGestureState (UIGestureRecognizerState state)
        {
            switch (state) {
            case UIGestureRecognizerState.Possible:
                return GestureState.Possible;
            case UIGestureRecognizerState.Began:
                return GestureState.Began;
            case UIGestureRecognizerState.Changed:
                return GestureState.Update;
            case UIGestureRecognizerState.Ended:
                return GestureState.Ended;
            case UIGestureRecognizerState.Cancelled:
                return GestureState.Cancelled;
            case UIGestureRecognizerState.Failed:
                return GestureState.Failed; 
            default:
                return GestureState.Failed;
            }    
        }
    }


using StatementsHere;
[assembly: ExportGesture (typeof(PanGesture), typeof(PanGestureRenderer))]
namespace YourNamespaceHere.iOS
{
public class PanGestureRenderer : BaseInteractiveGestureRenderer
{
    public PanGestureRenderer () : base ()
    {   
    }

    #region IGestureCreator implementation

    public override object Create (IInteractiveGestureRecognizer gesture, Element formsView, UIView nativeView)
    {   
        PanGesture panGesture = gesture as PanGesture; 
        nativeView.UserInteractionEnabled = true; 

        UIPanGestureRecognizer panGestureRecognizer = null;
        panGestureRecognizer = new UIPanGestureRecognizer (() => panGesture.Send());
    }
}
+1

All Articles