简体   繁体   English

UWP - 使用翻转视图缩放图像(使用捏缩放和双击)

[英]UWP - zooming image (with pinch zoom and double tap), with Flip View

I need to show images (using Flip View controll) and allow users to zoom them (with pinch zoom and with double tap) and drag zoomed image. 我需要显示图像(使用翻转视图控件)并允许用户缩放它们(使用捏缩放和双击)并拖动缩放图像。

I would like it to be compatible with Flip View (I mean it shouldn't ovveride draging gesture). 我希望它与Flip View兼容(我的意思是它不应该使用拖动手势)。

What is the best way to achieve that? 实现这一目标的最佳方法是什么?

I need to show images (using Flip View controll) and allow users to zoom them (with pinch zoom and with double tap) and drag zoomed image. 我需要显示图像(使用翻转视图控件)并允许用户缩放它们(使用捏缩放和双击)并拖动缩放图像。

We can use the ScrollViewer control and UIElement.DoubleTapped event to allow users to zoom the images (with pinch zoom and with double tap) and drag zoomed image. 我们可以使用ScrollViewer控件和UIElement.DoubleTapped事件来允许用户缩放图像(使用捏缩放和双击)并拖动缩放图像。

In order to zoom the image with pinch zoom and drag zoomed image. 使用捏拉缩放和拖动缩放图像缩放图像。 We can put the Image into the ScrollViewer . 我们可以将Image放入ScrollViewer

We can add UIElement.DoubleTapped event in the ScrollViewer to zoom the image with double tap. 我们可以在ScrollViewer添加UIElement.DoubleTapped事件,以双击缩放图像。

For example: 例如:

In XAML: 在XAML中:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <FlipView Name="MyFlipView">
            <FlipView.ItemTemplate>
                <DataTemplate x:DataType="local:MyImage">
                    <ScrollViewer  MinZoomFactor="1" DoubleTapped="scrollViewer_DoubleTapped"
                  ZoomMode="Enabled">
                        <Image Source="{Binding Path}" />
                    </ScrollViewer>
                </DataTemplate>
            </FlipView.ItemTemplate>
        </FlipView>
    </Grid>

In code behind: 代码背后:

public ObservableCollection<MyImage> MyImages;

public MainPage()
{
    this.InitializeComponent();
    MyImages = new ObservableCollection<MyImage>();
    MyImages.Add(new MyImage("ms-appx:///Assets/cliff.jpg"));
    MyImages.Add(new MyImage("ms-appx:///Assets/grapes.jpg"));
    MyImages.Add(new MyImage("ms-appx:///Assets/rainer.jpg"));
    MyImages.Add(new MyImage("ms-appx:///Assets/sunset.jpg"));
    MyImages.Add(new MyImage("ms-appx:///Assets/valley.jpg"));
    MyFlipView.ItemsSource = MyImages;
}

private async void scrollViewer_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
    var scrollViewer = sender as ScrollViewer;
    var doubleTapPoint = e.GetPosition(scrollViewer);

    if (scrollViewer.ZoomFactor != 1)
    {
        scrollViewer.ZoomToFactor(1);
    }
    else if (scrollViewer.ZoomFactor == 1)
    {
        scrollViewer.ZoomToFactor(2);

        var dispatcher = Window.Current.CoreWindow.Dispatcher;
        await dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
      {
          scrollViewer.ScrollToHorizontalOffset(doubleTapPoint.X);
          scrollViewer.ScrollToVerticalOffset(doubleTapPoint.Y);
      });
    }
}

And the MyImage Class code: 和MyImage类代码:

public class MyImage
{
    public MyImage()
    {
    }

    public MyImage(string uri)
    {
        this.Path = uri;
    }

    public string Path { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM