【发布时间】:2016-11-17 03:45:16
【问题描述】:
我是 Xamarin.Forms 的新手,我一直在尝试制作一个简单的程序。我想制作一个可以平移和缩放图像的程序。我在这个网站上找到了处理捏合手势和视图缩放的代码 (https://developer.xamarin.com/guides/xamarin-forms/user-interface/gestures/pinch/)。然后我尝试使用此代码来扩展“ScrollView”类。
当我在 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;
}
}
}
}
【问题讨论】:
-
你能解决这个问题吗?我有同样的问题..
-
同样的问题...有人可以帮忙...
-
你能解决这个问题吗?
标签: xamarin xamarin.forms