public class PanContainer : ContentView
{
double x, y;
public PanContainer ()
{
// Set PanGestureRecognizer.TouchPoints to control the
// number of touch points needed to pan
var panGesture = new PanGestureRecognizer ();
panGesture.PanUpdated += OnPanUpdated;
GestureRecognizers.Add (panGesture);
}
void OnPanUpdated (object sender, PanUpdatedEventArgs e)
{
switch (e.StatusType) {
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;
}
}
}
2条答案
按热度按时间wqsoz72f1#
你可以使用平移手势来实现它。这里有一个在PanContainer中 Package 图像的很好的例子-Adding a Pan Gesture Recognizer。
平移手势用于检测拖动,并通过PanGestureRecognizer类实现。平移手势的常见场景是水平和垂直拖动图像,以便当图像显示在小于图像尺寸的视口中时,可以查看所有图像内容。这是通过在视口中移动图像来实现的。
简单示例:
平移容器示例XAML:
后面的代码:
x9ybnkn62#
您可以尝试使用我在.NET Maui上制作并验证的this modification到TBertuzzi/Xamarin.Forms.PinchZoomImage,以便: