gpt4 book ai didi

wpf - WPFExtensions ZoomControl-捏缩放正在跳跃

转载 作者:行者123 更新时间:2023-12-02 01:10:17 29 4
gpt4 key购买 nike

我想使用WPFExtensions.Controls.ZoomControl缩放手势。

基于此answer-我知道如何检测捏手势。我将其用作_detector成员。我使用ZoomControl.ManipulationDelta进行缩放。

所以我想我只是像这样接受ManipulationDelta和Zoom:

private void AssociatedObject_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
{
if (_detector.IsScalingAllowed) // this just tells me if it is a pinch gesture or not
{
if ((e.DeltaManipulation.Scale.X < 1)
|| (e.DeltaManipulation.Scale.X > 1))
{
//AssociatedObject is ZoomControl
AssociatedObject.Zoom = Math.Max(e.DeltaManipulation.Scale.X, e.DeltaManipulation.Scale.Y);
}
}
}

但这只会使我的 ZoomControl缩放变得很差,前后跳转。

我完整的代码如下所示:
public class ZoomBoxBehaviour : Behavior<ZoomControl>
{
private GestureDetector _detector;

protected override void OnAttached()
{
base.OnAttached();
if (AssociatedObject != null)
{
_detector = new GestureDetector(AssociatedObject);

if (!AssociatedObject.IsManipulationEnabled) AssociatedObject.IsManipulationEnabled = true;

AssociatedObject.ManipulationDelta += AssociatedObject_ManipulationDelta;
}
}

protected override void OnDetaching()
{
if (AssociatedObject != null)
{
AssociatedObject.ManipulationDelta -= AssociatedObject_ManipulationDelta;
}

base.OnDetaching();
}

private void AssociatedObject_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
{
if (_detector.IsScalingAllowed) // this just tells me if it is a pinch gesture or not
{
if ((e.DeltaManipulation.Scale.X < 1)
|| (e.DeltaManipulation.Scale.X > 1))
{
AssociatedObject.Zoom = Math.Max(e.DeltaManipulation.Scale.X, e.DeltaManipulation.Scale.Y);
}
}
}
}

您可以从第一个答案中获得 GestureDetector here

更新

我将其正确缩放到中心
    private void AssociatedObject_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
{
if (_detector.IsScalingAllowed) // this just tells me if it is a pinch gesture or not
{
if ((e.DeltaManipulation.Scale.X < 1)
|| (e.DeltaManipulation.Scale.X > 1))
{
AssociatedObject.Zoom = Math.Max(e.CumulativeManipulation.Scale.X, e.CumulativeManipulation.Scale.Y);

//Size newSize = new Size(AssociatedObject.ZoomBox.Size.Width * e.CumulativeManipulation.Scale.X,
// AssociatedObject.ZoomBox.Size.Height * e.CumulativeManipulation.Scale.Y);

//AssociatedObject.ZoomTo(new Rect(new Point(0,0), newSize));
}
}


}

但是现在我想缩放到手势的中心,但是此注释的 code只会放大到最大....为什么?

最佳答案

我解决了

public class ZoomBoxBehaviour : Behavior<ZoomControl>
{
private GestureDetector _detector;
protected override void OnAttached()
{
base.OnAttached();
if (AssociatedObject != null)
{
_detector = new GestureDetector(AssociatedObject);

if (!AssociatedObject.IsManipulationEnabled) AssociatedObject.IsManipulationEnabled = true;

AssociatedObject.ManipulationDelta += AssociatedObject_ManipulationDelta;
}
}


protected override void OnDetaching()
{
if (AssociatedObject != null)
{
AssociatedObject.ManipulationDelta -= AssociatedObject_ManipulationDelta;
}

base.OnDetaching();
}

private void AssociatedObject_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
e.ManipulationContainer = ((FrameworkElement)e.Source).Parent as FrameworkElement;
}

private void AssociatedObject_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
{
if (_detector.IsPanningAllowed)
{

// Limit the X/Y translation extent to prevent the element from 'jumping' when using slow touchscreens, or many touch points.
const double translationThreshold = 100.0;

//Perform a translation(pan) tranformation
if ((e.DeltaManipulation.Translation.X < translationThreshold &&
e.DeltaManipulation.Translation.X > -translationThreshold)
)
{
AssociatedObject.TranslateX += e.DeltaManipulation.Translation.X;
}

if ((e.DeltaManipulation.Translation.Y < translationThreshold &&
e.DeltaManipulation.Translation.Y > -translationThreshold))
{
AssociatedObject.TranslateY += e.DeltaManipulation.Translation.Y;
}
}

if (_detector.IsScalingAllowed) // this just tells me if it is a pinch gesture or not
{
if ((e.CumulativeManipulation.Scale.X < 1)
|| (e.CumulativeManipulation.Scale.X > 1))
{
AssociatedObject.Zoom = Math.Max(e.CumulativeManipulation.Scale.X, e.CumulativeManipulation.Scale.Y);
}
}

}

}

关于wpf - WPFExtensions ZoomControl-捏缩放正在跳跃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45432256/

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