gpt4 book ai didi

windows - 捏缩放 - 获取触摸坐标

转载 作者:可可西里 更新时间:2023-11-01 13:47:30 24 4
gpt4 key购买 nike

我正在使用 WinJS 开发 Windows 8 应用程序。我正在尝试获取捏和缩放的触摸坐标。我已经通过 Windows.UI.Input.GestureRecognizer 实现了手势操作处理程序。当“manipulationupdated”事件 event.delta.scale 不是 1 时,我触发了捏合和缩放逻辑。当在“manipulationupdated”事件对象内部发生操作时,我发现只有 1 个位置的坐标。如何根据此信息计算手指触摸坐标?

另外我怎么知道这个位置属于哪个触摸坐标?我发现该位置在事件对象内重复多次 - 在 event.position 和 event.detail[0].position

我想要实现的是在图表(很像 map )中执行缩放。请帮我解决这些问题。

最佳答案

公共(public)类 PZBehavior :行为 {

    bool _isDragging;
bool _isPinching;
Point _ptPinchPositionStart;

private Image _imgZoom;
private ScaleTransform _scaleTransform;
private RotateTransform _rotateTransform;
private TranslateTransform _translateTransform;
private MatrixTransform _previousTransform;

private TransformGroup _parentGroup;
private TransformGroup _currentTransform;



protected override void OnAttached()
{
_imgZoom = AssociatedObject;
_imgZoom.RenderTransform = BuildTrasnformGroup();
var listener = GestureService.GetGestureListener(AssociatedObject);
listener.DragStarted += DragStarted;
listener.DragDelta += DragDelta;
listener.DragCompleted += DragCompleted;
listener.PinchStarted += PinchStarted;
listener.PinchDelta += PinchDelta;
listener.PinchCompleted += PinchCompleted;
}


private TransformGroup BuildTrasnformGroup()
{
_parentGroup = new TransformGroup();
_currentTransform = new TransformGroup();

_previousTransform = new MatrixTransform();

_scaleTransform = new ScaleTransform();
_rotateTransform = new RotateTransform();
_translateTransform = new TranslateTransform();

_currentTransform.Children.Add(_scaleTransform);
_currentTransform.Children.Add(_rotateTransform);
_currentTransform.Children.Add(_translateTransform);

_parentGroup.Children.Add(_previousTransform);
_parentGroup.Children.Add(_currentTransform);

return _parentGroup;

}


void PinchCompleted(object sender, PinchGestureEventArgs e)
{
if (_isPinching)
{
TransferTransforms();
_isPinching = false;
}
}

void PinchDelta(object sender, PinchGestureEventArgs e)
{
if (_isPinching)
{
// Set scaling
_scaleTransform.ScaleX = e.DistanceRatio;
_scaleTransform.ScaleY = e.DistanceRatio;

// Optionally set rotation

_rotateTransform.Angle = e.TotalAngleDelta;

// Set translation
Point ptPinchPosition = new Point(0,0);
_translateTransform.X = ptPinchPosition.X - _ptPinchPositionStart.X;
_translateTransform.Y = ptPinchPosition.Y - _ptPinchPositionStart.Y;
}
}

void PinchStarted(object sender, PinchStartedGestureEventArgs e)
{
_isPinching = e.OriginalSource == _imgZoom;

if (_isPinching)
{
// Set transform centers
Point ptPinchCenter = e.GetPosition(_imgZoom);
ptPinchCenter = _previousTransform.Transform(ptPinchCenter);

_scaleTransform.CenterX = ptPinchCenter.X;
_scaleTransform.CenterY = ptPinchCenter.Y;

_rotateTransform.CenterX = ptPinchCenter.X;
_rotateTransform.CenterY = ptPinchCenter.Y;

_ptPinchPositionStart = new Point(0,0);
}
}

void DragCompleted(object sender, DragCompletedGestureEventArgs e)
{
if (_isDragging)
{
TransferTransforms();
_isDragging = false;
}
}

void DragDelta(object sender, DragDeltaGestureEventArgs e)
{
if (_isDragging)
{
_translateTransform.X += e.HorizontalChange;
_translateTransform.Y += e.VerticalChange;
}
}

void DragStarted(object sender, DragStartedGestureEventArgs e)
{
_isDragging = e.OriginalSource == _imgZoom;

}

void TransferTransforms()
{
_previousTransform.Matrix = Multiply(_previousTransform.Matrix, _currentTransform.Value);

// Set current transforms to default values
_scaleTransform.ScaleX = _scaleTransform.ScaleY = 1;
_scaleTransform.CenterX = _scaleTransform.CenterY = 0;

_rotateTransform.Angle = 0;
_rotateTransform.CenterX = _rotateTransform.CenterY = 0;

_translateTransform.X = _translateTransform.Y = 0;
}

Matrix Multiply(Matrix a, Matrix b)
{
return new Matrix(a.M11 * b.M11 + a.M12 * b.M21,
a.M11 * b.M12 + a.M12 * b.M22,
a.M21 * b.M11 + a.M22 * b.M21,
a.M21 * b.M12 + a.M22 * b.M22,
a.OffsetX * b.M11 + a.OffsetY * b.M21 + b.OffsetX,
a.OffsetX * b.M12 + a.OffsetY * b.M22 + b.OffsetY);
}
}

关于windows - 捏缩放 - 获取触摸坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12892762/

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