【问题标题】:xamarin forms zoomable image (Cross Platform)xamarin 形成可缩放图像(跨平台)
【发布时间】:2017-09-14 11:37:08
【问题描述】:

有没有办法使用pinch来放大共享的Xamarin Forms,我只找到了每个平台的实现。

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    您可以使用平移手势来实现它。 这里有一个很好的在 PanContainer 中包装图像的示例 - Adding a Pan Gesture Recognizer

    平移手势用于检测拖动,并通过 PanGestureRecognizer 类。平移手势的常见场景 是水平和垂直拖动图像,使所有的 图像内容在视口中显示时可以查看 小于图像尺寸。这是通过移动 视口内的图像。

    简单示例:

    <Image Source="MonoMonkey.jpg">
      <Image.GestureRecognizers>
        <PanGestureRecognizer PanUpdated="OnPanUpdated" />
      </Image.GestureRecognizers>
    </Image>
    

    Pan Container 示例 XAML:

    <AbsoluteLayout>
        <local:PanContainer>
            <Image Source="MonoMonkey.jpg" WidthRequest="1024" HeightRequest="768" />
        </local:PanContainer>
    </AbsoluteLayout>
    

    后面的代码:

    public class PanContainer : ContentView
    {
      double x, y;
    
      public PanContainer ()
      {
        // Set PanGestureRecognizer.TouchPoints to control the
        // number of touch points needed to pan
        var panGesture = new PanGestureRecognizer ();
        panGesture.PanUpdated += OnPanUpdated;
        GestureRecognizers.Add (panGesture);
      }
    
     void OnPanUpdated (object sender, PanUpdatedEventArgs e)
     {
       switch (e.StatusType) {
       case GestureStatus.Running:
         // Translate and ensure we don't pan beyond the wrapped user interface element bounds.
         Content.TranslationX =
          Math.Max (Math.Min (0, x + e.TotalX), -Math.Abs (Content.Width - App.ScreenWidth));
         Content.TranslationY =
          Math.Max (Math.Min (0, y + e.TotalY), -Math.Abs (Content.Height - App.ScreenHeight));
         break;
    
       case GestureStatus.Completed:
         // Store the translation applied during the pan
         x = Content.TranslationX;
         y = Content.TranslationY;
         break;
       }
     }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-05
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 2020-03-08
      • 2019-01-25
      • 2017-06-26
      相关资源
      最近更新 更多