如何检测两个同时触摸? [英] How to detect two simultaneous touches?

查看:37
本文介绍了如何检测两个同时触摸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows Phone 8 中检测触摸利用 System.Windows.Input.Touch.FrameReported 事件,这是开发人员可用的最原始,当然也是响应最快的触摸事件.

Detecting touch in Windows Phone 8 leverages the System.Windows.Input.Touch.FrameReported event which is the most raw and certainly the most responsive touch event available to developers.

您可以像这样使用事件:

You would use the event like this:

public MainPage()
{
    InitializeComponent();

    // setup sounds
    Ellipse1.Tag = new Uri("Sounds/GVD_snr1.wav", UriKind.Relative);
    Ellipse2.Tag = new Uri("Sounds/GVD_snr2.wav", UriKind.Relative);
    Ellipse3.Tag = new Uri("Sounds/GVD_snr3.wav", UriKind.Relative);
    Ellipse4.Tag = new Uri("Sounds/GVD_snr4.wav", UriKind.Relative);
    Ellipse5.Tag = new Uri("Sounds/GVD_snr5.wav", UriKind.Relative);
    Ellipse6.Tag = new Uri("Sounds/GVD_snr6.wav", UriKind.Relative);
    Ellipse7.Tag = new Uri("Sounds/Gong.wav", UriKind.Relative);

    // respond to touch(es)
    var _Ellipses = new[] { Ellipse1, Ellipse2, Ellipse3, Ellipse4, Ellipse5, Ellipse6, Ellipse7 };
    System.Windows.Input.Touch.FrameReported += (s, e) =>
    {
        var _Touches =
            from touch in e.GetTouchPoints(null)
            where touch.Action == System.Windows.Input.TouchAction.Down
            let ellipse = touch.TouchDevice.DirectlyOver as Ellipse
            where _Ellipses.Contains(ellipse)
            select ellipse;
        System.Diagnostics.Debug.WriteLine("{0} touch(es).", _Touches.Count());
        foreach (var ellipse in _Touches)
        {
            var _Stream = Application.GetResourceStream(ellipse.Tag as Uri).Stream;
            var _SoundEffect = Microsoft.Xna.Framework.Audio.SoundEffect.FromStream(_Stream);
            Microsoft.Xna.Framework.FrameworkDispatcher.Update();
            _SoundEffect.Play();
        }
    };
}

(使用 Lumia 920 测试)

(tested with a Lumia 920)

这就像一个魅力 - 只要一次只有一次触摸.当用户尝试同时触摸两个或更多点(我的意思是完全相同的时间)时,根本不会引发事件.当用户尝试几乎同时触摸两个或多个点(仅相隔一瞬间)时,将引发事件并报告两个点.

This works like a charm - as long as there is only a single touch at a time. When the user attempts to touch two or more points simultaneously (and I mean exactly the same time) the event is not raised at all. When the user attempts to touch two or more points almost simultaneously (just a split second apart) then the event is raised and both points are reported.

如何检测两个同时触摸?

如果您想查看 XAML,这里是 XAML:

In case you want to see the XAML, here's the XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

    <Grid.Resources>
        <Style TargetType="Ellipse">
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="VerticalAlignment" Value="Top" />
        </Style>
    </Grid.Resources>

    <Ellipse x:Name="Ellipse1" Fill="Blue" Height="177" Margin="17,17,0,0" Width="177"/>
    <Ellipse x:Name="Ellipse2" Fill="#FFFFA300" Height="223" Margin="212,25,0,0" Width="223"/>
    <Ellipse x:Name="Ellipse3" Fill="#FFFF00E8" Height="97" Margin="89,207,0,0" Width="97"/>
    <Ellipse x:Name="Ellipse4" Fill="#FF00C135" Height="162" Margin="186,249,0,0" Width="162"/>
    <Ellipse x:Name="Ellipse5" Fill="#FF00AEFF" Height="272" Margin="59,416,0,-81" Width="272"/>
    <Ellipse x:Name="Ellipse6" Fill="Red" Height="97" Margin="320,395,0,0" Width="97"/>
    <Ellipse x:Name="Ellipse7" Fill="#FFF3FF00" Height="133" Margin="10,304,0,0" Width="133"/>

</Grid>

推荐答案

添加这个解决了问题:

var _Timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(50) };
_Timer.Tick += (s, e) =>
{
    try { Microsoft.Xna.Framework.FrameworkDispatcher.Update(); }
    catch { }
};
_Timer.Start();

这篇关于如何检测两个同时触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆