2015-05-04 71 views

回答

2

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

平移手势用于检测拖动,并使用 PanGestureRecognizer类实现。平移手势 的一种常见方案是水平和垂直拖动图像,以便在图像尺寸小于视图尺寸的视口中显示图像内容时,可以查看所有图像内容。这是通过在视口内移动 图像来完成的。

简单的例子:

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

潘集装箱例如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; 
    } 
} 
}