Xamarin在视图中形成PanGesture

时间:2017-07-21 15:32:51

标签: c# xamarin.forms gesture

我在Xamarin表格中遇到PanGestuer行为问题。这是我的页面:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:Xamarin_Test"
         x:Class="Xamarin_Test.MainPage">
  <StackLayout x:Name="parentFrame" Margin="100" BackgroundColor="Gray">
    <AbsoluteLayout x:Name="parentLayout" BackgroundColor="Red">
        <local:PanContainer x:Name="parentContainer" BackgroundColor="Yellow">
            <Image x:Name="panImage" Source="MonoMonkey.jpg"
                       WidthRequest="{Binding Path=Width, Source={x:Reference Name=parentFrame}}"
                       HeightRequest="{Binding Path=Height, Source={x:Reference Name=parentFrame}}"/>
        </local:PanContainer>
    </AbsoluteLayout>
  </StackLayout>
</ContentPage>

PanContainer.cs

public class PanContainer : ContentView
{
    double x, y;

    public PanContainer()
    {
        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;
        }
    }
}

图像加载的大小正确(在StackLayout边框内,为了更好的测试,我将元素着色)。平移工作在图像的左侧和上方,但覆盖白色区域。如何处理平移,以便移动的图像仅在我的panContainer

中可见

0 个答案:

没有答案