我发布了另一个问题,关于如何通过监控TouchDown事件的两次触摸之间的时间间隔来“手动”捕获双击,但它存在相当多的but。有没有人知道微软在多点触摸屏上捕获双击的标准方法/事件?
非常感谢,
丹
发布于 2012-10-16 10:41:35
我检查了敲击位置和秒表的组合,它工作得很完美!
private readonly Stopwatch _doubleTapStopwatch = new Stopwatch();
private Point _lastTapLocation;
public event EventHandler DoubleTouchDown;
protected virtual void OnDoubleTouchDown()
{
if (DoubleTouchDown != null)
DoubleTouchDown(this, EventArgs.Empty);
}
private bool IsDoubleTap(TouchEventArgs e)
{
Point currentTapPosition = e.GetTouchPoint(this).Position;
bool tapsAreCloseInDistance = currentTapPosition.GetDistanceTo(_lastTapLocation) < 40;
_lastTapLocation = currentTapPosition;
TimeSpan elapsed = _doubleTapStopwatch.Elapsed;
_doubleTapStopwatch.Restart();
bool tapsAreCloseInTime = (elapsed != TimeSpan.Zero && elapsed < TimeSpan.FromSeconds(0.7));
return tapsAreCloseInDistance && tapsAreCloseInTime;
}
private void OnPreviewTouchDown(object sender, TouchEventArgs e)
{
if (IsDoubleTap(e))
OnDoubleTouchDown();
}
它在PreviewTouchDown中检查它是否是DoubleTap。
发布于 2015-04-13 09:11:14
Jowens answer对我帮助很大(如果我的声誉允许,我会给它加码;)但我必须调整它,所以它只需双击就能工作。原始代码确实认为任何超过2的轻敲都是双击。将_lastTapLocation更改为可空并在双击时将其重置会有所帮助。
private Point? _lastTapLocation;
private bool IsDoubleTap(TouchEventArgs e)
{
Point currentTapPosition = e.GetTouchPoint(this).Position;
bool tapsAreCloseInDistance = false;
if (_lastTapLocation != null)
{
tapsAreCloseInDistance = GetDistanceBetweenPoints(currentTapPosition, (Point)_lastTapLocation) < 70;
}
_lastTapLocation = currentTapPosition;
TimeSpan elapsed = _doubleTapStopwatch.Elapsed;
_doubleTapStopwatch.Restart();
bool tapsAreCloseInTime = (elapsed != TimeSpan.Zero && elapsed < TimeSpan.FromSeconds(0.7));
if (tapsAreCloseInTime && tapsAreCloseInDistance)
{
_lastTapLocation = null;
}
return tapsAreCloseInDistance && tapsAreCloseInTime;
}
发布于 2016-06-09 11:54:02
我认为利用StylusSystemGesture事件更合适。这是我的代码。
public static class ext
{
private static Point? _lastTapLocation;
private static readonly Stopwatch _DoubleTapStopwatch = new Stopwatch();
public static bool IsDoubleTap(this StylusSystemGestureEventArgs e, IInputElement iInputElement)
{
Point currentTapPosition = e.GetPosition(iInputElement);
bool tapsAreCloseInDistance = false;
if (_lastTapLocation != null)
{
tapsAreCloseInDistance = GetDistanceBetweenPoints(currentTapPosition, (Point)_lastTapLocation) < 70;
}
_lastTapLocation = currentTapPosition;
TimeSpan elapsed = _DoubleTapStopwatch.Elapsed;
_DoubleTapStopwatch.Restart();
bool tapsAreCloseInTime = (elapsed != TimeSpan.Zero && elapsed < TimeSpan.FromSeconds(0.7));
if (tapsAreCloseInTime && tapsAreCloseInDistance)
{
_lastTapLocation = null;
}
return tapsAreCloseInDistance && tapsAreCloseInTime;
}
}
用法:
private void UIElement_OnStylusSystemGesture(object sender, StylusSystemGestureEventArgs e)
{
if (e.SystemGesture == SystemGesture.Tap)
{
if (e.IsDoubleTap(sender as IInputElement))
{
// Do your stuff here
}
}
}
https://stackoverflow.com/questions/9001023
复制