gpt4 book ai didi

xamarin.forms - 如何在 SKCanvasView 上实现双指缩放和滑动?

转载 作者:行者123 更新时间:2023-12-04 01:10:55 25 4
gpt4 key购买 nike

我正在使用 xamarin 表单开发图像查看器。我的源图像是无限大的,比我的视口(viewport)大得多。所以我使用 SKCanvasView 控件逐部分绘制,以便我应该在图像中滑动。

我可以通过 SKCanvasView 上可用的 Touch 事件获得 Touch 点。在这里,我能够检测到双击。滑动和平移。

我无法决定何时向上/向下滑动需要向上/向下移动多少图像?

最初,我尝试将 SKCanvasView 放置在 ScrollView 控件之上,并使用我绘制 SKCanvas 的滚动位置值。这里滚动和打印效果完美。但是在这里,双指缩放不起作用,因为双指手势事件没有触发,而是在滚动。

最佳答案

虽然我个人从未使用过 Skia,但我曾经为我的一位 friend 做过类似的 SKCanvas。

无论如何,它都有 Pinch、Pan 和 Tap 缩放事件,您可以根据需要随意进行任何更改:

 public class CustomSKCanvas : SKCanvasView
{
private const double MIN_SCALE = 1;
private const double MAX_SCALE = 4;
private const double OVERSHOOT = 0.15;
private double StartScale, LastScale;
private double StartX, StartY;

public CustomSKCanvas()
{
var pinch = new PinchGestureRecognizer();
pinch.PinchUpdated += OnPinchUpdated;
GestureRecognizers.Add(pinch);

var pan = new PanGestureRecognizer();
pan.PanUpdated += OnPanUpdated;
GestureRecognizers.Add(pan);

var tap = new TapGestureRecognizer { NumberOfTapsRequired = 2 };
tap.Tapped += OnTapped;
GestureRecognizers.Add(tap);

Scale = MIN_SCALE;
TranslationX = TranslationY = 0;
AnchorX = AnchorY = 0;
}

protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
{
Scale = MIN_SCALE;
TranslationX = TranslationY = 0;
AnchorX = AnchorY = 0;
return base.OnMeasure(widthConstraint, heightConstraint);
}

private void OnTapped(object sender, EventArgs e)
{
if (Scale > MIN_SCALE)
{
this.ScaleTo(MIN_SCALE, 250, Easing.CubicInOut);
this.TranslateTo(0, 0, 250, Easing.CubicInOut);
}
else
{
AnchorX = AnchorY = 0.5; //TODO tapped position
this.ScaleTo(MAX_SCALE, 250, Easing.CubicInOut);
}
}

private void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
switch (e.StatusType)
{
case GestureStatus.Started:
StartX = (1 - AnchorX) * Width;
StartY = (1 - AnchorY) * Height;
break;
case GestureStatus.Running:
AnchorX = Clamp(1 - (StartX + e.TotalX) / Width, 0, 1);
AnchorY = Clamp(1 - (StartY + e.TotalY) / Height, 0, 1);
break;
}
}

private void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
{
switch (e.Status)
{
case GestureStatus.Started:
LastScale = e.Scale;
StartScale = Scale;
AnchorX = e.ScaleOrigin.X;
AnchorY = e.ScaleOrigin.Y;
break;
case GestureStatus.Running:
if (e.Scale < 0 || Math.Abs(LastScale - e.Scale) > (LastScale * 1.3) - LastScale)
{ return; }
LastScale = e.Scale;
var current = Scale + (e.Scale - 1) * StartScale;
Scale = Clamp(current, MIN_SCALE * (1 - OVERSHOOT), MAX_SCALE * (1 + OVERSHOOT));
break;
case GestureStatus.Completed:
if (Scale > MAX_SCALE)
this.ScaleTo(MAX_SCALE, 250, Easing.SpringOut);
else if (Scale < MIN_SCALE)
this.ScaleTo(MIN_SCALE, 250, Easing.SpringOut);
break;
}
}

private T Clamp<T>(T value, T minimum, T maximum) where T : IComparable
{
if (value.CompareTo(minimum) < 0)
return minimum;
else if (value.CompareTo(maximum) > 0)
return maximum;
else
return value;
}
}

祝你好运

如有疑问,请回复。

关于xamarin.forms - 如何在 SKCanvasView 上实现双指缩放和滑动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55493569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com