【发布时间】:2017-03-04 00:39:27
【问题描述】:
我已经分别实现了平移和捏合,效果很好。我现在正在尝试同时使用捏和平移,但我发现了一些问题。这是我的代码:
XAML:
<AbsoluteLayout x:Name="PinchZoomContainer">
<controls:NavBar x:Name="NavBar" ShowPrevNext="true" ShowMenu="false" IsModal="true" />
<controls:PanContainer x:Name="PinchToZoomContainer">
<Image x:Name="ImageMain" />
</controls:PanContainer>
</AbsoluteLayout>
捏/平移手势添加:
var panGesture = new PanGestureRecognizer();
panGesture.PanUpdated += OnPanUpdated;
GestureRecognizers.Add(panGesture);
var pinchGesture = new PinchGestureRecognizer();
pinchGesture.PinchUpdated += OnPinchUpdated;
GestureRecognizers.Add(pinchGesture);
平移方法:
void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
switch (e.StatusType)
{
case GestureStatus.Started:
startX = e.TotalX;
startY = e.TotalY;
Content.AnchorX = 0;
Content.AnchorY = 0;
break;
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;
}
}
捏法:
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;
//ImageMain.AnchorX = 0;
//ImageMain.AnchorY = 0;
}
if (e.Status == GestureStatus.Running)
{
// Calculate the scale factor to be applied.
currentScale += (e.Scale - 1) * startScale;
currentScale = Math.Max(1, currentScale);
currentScale = Math.Min(currentScale, 2.5);
// 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) 锅实际上似乎按预期工作 2)当您最初平移图像时,假设将图像移动到 Y 中心和 X 中心,然后尝试缩放,图像被设置回其初始状态。然后,当您平移时,它会将您移回您尝试缩放之前的位置(这就是我说平移工作正常的原因)。
根据我从调试中了解到的情况,当您缩放时并没有考虑到您当前所处的位置。因此,当您先平移然后缩放时,它不会缩放您所在的位置,而是缩放图像的起点。然后,当您尝试从那里平移时,平移方法仍会记住您的位置,并将您移回您尝试缩放之前的位置。
希望对此有所了解。显然,我的捏法存在问题。我只是认为(显然无法弄清楚)我需要在其中添加逻辑,以考虑您当前所处的位置。
【问题讨论】:
-
你最终让它工作了吗?
-
是的,我做了,最终做了一些与此不同的事情来让它工作。我将在下面发布我的方法。
-
非常感谢。我会试试这个。
-
如果它适合您,请点赞:) 如果您有任何问题,也很乐意为您提供帮助。
标签: xamarin xamarin.forms pinchzoom