Xamarin.Forms 在 ScrollView 问题中捏合手势

标签 xamarin xamarin.forms

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

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

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

当我将 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;
        }
    }
}

}

最佳答案

答案在于从事 Android、IOS 项目。

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

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

关于Xamarin.Forms 在 ScrollView 问题中捏合手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40646376/

相关文章:

xamarin - Mvvmcross:与 MvxLang 的文本格式绑定(bind)

c# - 如何正确绑定(bind)和更新 Xamarin.Forms ListView?

c# - Xamarin ProgressBar 没有实时更新

ios - iOS 9 之前的 Xamarin UIStackView

xamarin 从十六进制值设置 View 背景颜色

c# - Xamarin 中的 TapGesture From Bound 属性

c# - xamarin.droid 中 UITest 的引用问题

image - 使用 SkiaSharp 调整大小时 iPhone 图像方向错误

c# - 在 LockState 中通过 PhoneScreen 显示 Toast

ios - Xamarin.Forms - iOS - 在 UIButton 上放置多个图像的自定义效果