如何处理多点触摸在Windows

如何处理多点触摸在Windows

本文介绍了如何处理多点触摸在Windows Phone 8应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到一个例子如何处理多触摸在windows phone 8应用程序(在游戏中的c ++和directx)。
任何人都可以帮助我吗?
谢谢!

I can not find an example how to handle multi touch in windows phone 8 app ( in game on c++ and directx ).May be anyone can help me?Thanks!

推荐答案

我想出了如何做。我是一个C ++新手,所以可能有更好的方法来做到这一点,但我的方式工作。

I figured out how to do it. I am a C++ newbie so there may be much better ways to do this, but my way works.

(FYI - 我在诺基亚的开发者网站上写了一篇文章: )

(FYI - I wrote an article about this on Nokia's Developer site: http://developer.nokia.com/Community/Wiki/Multi-touch_handling_using_DirectX_C%2B%2B#PointerPoint )

您需要跟踪PointerPoint内部的PointerId。我将PointerId存储在std :: unordered_map中的OnPointerPressed事件中。键(unsigned int)是PointerId。然后,当您获取OnPointerReleased事件时,从地图中删除它们。 PointerId将匹配。事实证明,对于每一个新的手指,你会得到一个新的PointerId ...即使你释放你的第一个手指,把它再次下降。

You need to keep track of the PointerId inside of the PointerPoint. I store the PointerId in the OnPointerPressed event in a std::unordered_map. The key (unsigned int) is the PointerId. You then erase them from the map when you get the OnPointerReleased event. The PointerId will match up. It turns out that for every new finger, you get a new PointerId... even if you release your first finger and put it back down again.

然后,当您访问OnPointerMoved事件处理程序时,您只需调用unordered_map上的size()函数,即可了解当前屏幕上有多少触摸。

Then, when you get to your OnPointerMoved event handler, you just have to call the size() function on your unordered_map to find out how many touches you currently have on screen.

我使用unordered_map的原因是因为我需要跟踪以前的点位置。我意识到你可以使用GetIntermediatePoints方法获取旧的位置,但我不想要解决一些新的东西...

The reason I'm using the unordered_map is because I needed to keep track of previous point positions. I realize that you can probably get old positions using the GetIntermediatePoints method, but I didn't want to have to troubleshoot something new...

无论如何,希望这有帮助。代码如下:

Anyways, hope this helps. Code below:

EDIT:代码已更新,可处理每次触摸每个框架时获得的x移动事件。就像我说的,这不是最好的,但它工作。

Code updated to handle the x move events you get from each touch for each frame. Like I said, this isn't the best, but it works.

#include <unordered_map>
std::unordered_map<unsigned int, Windows::UI::Input::PointerPoint^> m_pointerIds;
std::unordered_map<unsigned int, Windows::UI::Input::PointerPoint^> m_oldPoints;

void Direct3DBackground::OnPointerPressed(DrawingSurfaceManipulationHost^ sender, PointerEventArgs^ args)
{
    m_pointerIds.emplace(args->CurrentPoint->PointerId, args->CurrentPoint);
    m_oldPoints.emplace(args->CurrentPoint->PointerId, args->CurrentPoint);
}

void Direct3DBackground::OnPointerMoved(DrawingSurfaceManipulationHost^ sender, PointerEventArgs^ args)
{
    if (m_pointerIds.size() == 1)
    {
        DoSomethingForOneTouch(args);
    }
    else if (m_pointerIds.size() == 2)
    {
        UINT changedPointId = args->CurrentPoint->PointerId;
        UINT frameId = args->CurrentPoint->FrameId;

        UINT otherPointId;
        for (auto it = m_pointerIds.begin(); it != m_pointerIds.end(); ++it)
        {
            if (it->first != changedPointId)
            {
                otherPointId = it->first;
                break;
            }
         }

         m_oldPoints[changedPointId] = m_pointerIds[changedPointId];
         m_pointerIds[changedPointId] = args->CurrentPoint;

         if (m_pointerIds[otherPointId]->FrameId == frameId)
         {
                  //DO YOUR CALCULATION using new points and old points
                  //as both touches have been updated for the current frame.
         }
    }
}

void Direct3DBackground::OnPointerReleased(DrawingSurfaceManipulationHost^ sender, PointerEventArgs^ args)
{
    m_pointerIds.erase(args->CurrentPoint->PointerId);
        m_oldPoints.erase(args->CurrentPoint->PointerId);
}

这篇关于如何处理多点触摸在Windows Phone 8应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 00:07