ScamaView问题中的Xamarin.Forms Pinch Gesture

时间:2016-11-17 03:45:17

标签: xamarin xamarin.forms

我是Xamarin.Forms的新手,我一直试图制作一个简单的程序。我想制作一个可以平移和缩放图像的程序。我在这个网站上找到了处理捏合手势和视图缩放的代码(https://developer.xamarin.com/guides/xamarin-forms/user-interface/gestures/pinch/)。然后我尝试使用此代码来扩展“ScrollView”#39;类。

当我在iOS上运行我的代码时,它在模拟器中运行良好。 (缩放比较笨重,但我不知道它是模拟器还是技术)

当我在Android上运行我的代码(模拟器或实际设备)时,页面只会滚动。在模拟器上,我使用命令键来调用捏合手势。我看到捏合手势的适当界面出现了,但看起来滚动视图正在截取手势作为平移手势。

当我把ScrollView类带出混合(并简单地使它成为ContentView类)时,捏合手势在两个平台上都能正常工作。

有什么想法吗?

这是我的容器的代码(几乎是上面链接代码的精确副本)

using System;
using Xamarin.Forms;

namespace ImageTest
{
public class PinchToZoomContainer : ScrollView
{
double currentScale = 1;
double startScale = 1;
double xOffset = 0;
double yOffset = 0;

    public PinchToZoomContainer()
    {
        var pinchGesture = new PinchGestureRecognizer();
        pinchGesture.PinchUpdated += OnPinchUpdated;
        GestureRecognizers.Add(pinchGesture);
    }

    public void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
    {
        if (e.Status == GestureStatus.Started)
        {
            // Store the current scale factor applied to the wrapped user interface element,
            // and zero the components for the center point of the translate transform.
            startScale = Content.Scale;
            Content.AnchorX = 0;
            Content.AnchorY = 0;
        }
        if (e.Status == GestureStatus.Running)
        {
            // Calculate the scale factor to be applied.
            currentScale += (e.Scale - 1) * startScale;
            currentScale = Math.Max(0.1, currentScale);

            // The ScaleOrigin is in relative coordinates to the wrapped user interface element,
            // so get the X pixel coordinate.
            double renderedX = Content.X + xOffset;
            double deltaX = renderedX / Width;
            double deltaWidth = Width / (Content.Width * startScale);
            double originX = (e.ScaleOrigin.X - deltaX) * deltaWidth;

            // The ScaleOrigin is in relative coordinates to the wrapped user interface element,
            // so get the Y pixel coordinate.
            double renderedY = Content.Y + yOffset;
            double deltaY = renderedY / Height;
            double deltaHeight = Height / (Content.Height * startScale);
            double originY = (e.ScaleOrigin.Y - deltaY) * deltaHeight;

            // Calculate the transformed element pixel coordinates.
            double targetX = xOffset - (originX * Content.Width) * (currentScale - startScale);
            double targetY = yOffset - (originY * Content.Height) * (currentScale - startScale);

            // Apply translation based on the change in origin.
            Content.TranslationX = targetX.Clamp(-Content.Width * (currentScale - 1), 0);
            Content.TranslationY = targetY.Clamp(-Content.Height * (currentScale - 1), 0);

            // Apply scale factor
            Content.Scale = currentScale;
        }
        if (e.Status == GestureStatus.Completed)
        {
            // Store the translation delta's of the wrapped user interface element.
            xOffset = Content.TranslationX;
            yOffset = Content.TranslationY;
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

答案在于 Android 和 IOS 项目。

查看本指南:http://www.xamboy.com/2017/08/02/creating-a-zoomable-scrollview-in-xamarin-forms/

建议创建一个 android 和 ios 平台支持的渲染,以便能够放大 ScrollView 上的应用程序。