xamarin形成可缩放的图像(跨平台)

时间:2015-05-04 07:07:43

标签: xamarin xamarin.forms

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

1 个答案:

答案 0 :(得分:2)

您可以使用Pan Gesture来实现它。 这里有一个很好的包装PanContainer图像样本 - Adding a Pan Gesture Recognizer

  

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

简单示例:

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