iPhone:跟踪/识别个人触摸 [英] iPhone: Tracking/Identifying individual touches

查看:111
本文介绍了iPhone:跟踪/识别个人触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在iPhone上跟踪触摸的快速问题,我似乎无法得出结论,所以任何建议/想法都非常感激:

I have a quick question regarding tracking touches on the iPhone and I seem to not be able to come to a conclusion on this, so any suggestions / ideas are greatly appreciated:

我想要能够跟踪和识别iphone上的触摸,即。基本上每次触摸都具有开始位置和当前/移动的位置。触点存储在std :: vector中,一旦它们结束,它们将从容器中删除。他们的位置将被更新一旦他们移动,但我仍然想跟踪他们最初开始(手势识别)。

I want to be able to track and identify touches on the iphone, ie. basically every touch has a starting position and a current/moved position. Touches are stored in a std::vector and they shall be removed from the container, once they ended. Their position shall be updated once they move, but I still want to keep track of where they initially started (gesture recognition).

我得到的触摸从[事件allTouches ],事情是,NSSet是未排序的,我似乎无法识别已经存储在std :: vector中的触摸,并参考NSSet中的触摸(所以我知道哪些结束了,应该删除,或已被移动等)

I am getting the touches from [event allTouches], thing is, the NSSet is unsorted and I seem not to be able to identify the touches that are already stored in the std::vector and refer to the touches in the NSSet (so I know which ones ended and shall be removed, or have been moved, etc.)

这是我的代码,当然只有一个手指在触摸屏上工作,当然,但有多个,我获得不可预测的结果...

Here is my code, which works perfectly with only one finger on the touch screen, of course, but with more than one, I do get unpredictable results...

    - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    [self handleTouches:[event allTouches]];
}

- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
    [self handleTouches:[event allTouches]];
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    [self handleTouches:[event allTouches]];
}

- (void) touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event
{
    [self handleTouches:[event allTouches]];
}

- (void) handleTouches:(NSSet*)allTouches
{   
    for(int i = 0; i < (int)[allTouches count]; ++i)
    {
        UITouch* touch = [[allTouches allObjects] objectAtIndex:i];
        NSTimeInterval timestamp = [touch timestamp];

        CGPoint currentLocation = [touch locationInView:self];
        CGPoint previousLocation = [touch previousLocationInView:self];

        if([touch phase] == UITouchPhaseBegan)
        {
            Finger finger;
            finger.start.x = currentLocation.x;
            finger.start.y = currentLocation.y;
            finger.end = finger.start;
            finger.hasMoved = false;
            finger.hasEnded = false;

            touchScreen->AddFinger(finger);
        }
        else if([touch phase] == UITouchPhaseEnded || [touch phase] == UITouchPhaseCancelled)
        {
            Finger& finger = touchScreen->GetFingerHandle(i);

            finger.hasEnded = true;
        }
        else if([touch phase] == UITouchPhaseMoved)
        {
            Finger& finger = touchScreen->GetFingerHandle(i);

            finger.end.x = currentLocation.x;
            finger.end.y = currentLocation.y;
            finger.hasMoved = true;
        }
    }

    touchScreen->RemoveEnded();
}

谢谢!

推荐答案

要解决你的问题,你的handleTouches方法。你在handleTouches方法中做的第一件事是将其切换到touchPhase上,但这已经给你。如果你收到在touchesBegan的触摸,你知道触摸是在UITouchPhaseBegan。

To fix your problem scrap your "handleTouches" method. The first thing you do in your handleTouches method, is switch it on the touchPhase, but that is already given to you. If you recieve the touch in touchesBegan, you know the touch is in UITouchPhaseBegan. By funneling touches from the four touch methods into one method, you are defeating the purpose of having four delegate methods.

在这四种方法中,苹果给你一个机会,处理当前触摸的不同阶段。

In each of those methods, Apple gives you an opportunity to deal with a different phase of the current touch.

第二件事是,您不需要搜索当前触摸的事件,它被给予您参数:touches。

The second thing is that you don't need to search the event for the current touch, it is given to you as a parameter: touches.

事件由多组触摸组成。

An event is comprised of sets of touches. For convienence, you are given the current touches even though it can also be found within event.

因此,在touchesBegan,你开始跟踪一个触摸。

So, in touchesBegan, you start tracking a touch.

    - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{


        NSString *startPoint = NSStringFromCGPoint([[touches anyObject] locationInView:self]);  

        NSDictionary * touchData = [NSDictionary dictionaryWithObjectsandKeys: startPoint, @"location", touches, @"touch"]

        [startingLocations addObject:touchData];

        }

我使用一个字典数组数据。

I'm using an array of dictionaries to hold my touch data.

尝试分离您的代码,并将其移动到适当的触摸方法。对于方向,苹果有几个示例项目,专注于触摸,并告诉你如何设置这些方法。

Try to seperate your code and move it into the appropriate touch method. For direction, Apple has a couple sample projects that focus on touches and show you how to setup those methods.

请记住,这些方法将被自动调用每个触摸在每个阶段,你不需要通过事件循环来发现发生了什么。

Remember, these methods will get called automatically for each touch during each phase, you don't need to cycle through the event to find out what happened.

每组触摸的指针保持不变,只是数据改变。

The pointer to each set of touches remains constant, just the data changes.

此外,我将阅读关于事件处理的iPhone OS编程指南部分,该部分涉及我上面所说的更深入的几个图,解释了触摸与事件的关系

Also, I would read the iPhone OS programming guide section on event handling which goes into greater depth of what I said above with several diagrams explaining the relationship of touches to events over time.



An excerpt:


在iPhone操作系统中,UITouch对象
表示触摸,UIEvent
对象表示事件。事件
对象包含用于
当前多触摸序列的所有触摸对象,
可以提供特定于
视图或窗口的触摸对象(参见图3-2)。 A
触摸对象在序列期间对于给定的
手指是持续的,并且UIKit
使其变化,因为它在其中跟踪手指
。改变的触摸属性

touch的阶段,它在视图中的位置,它的
之前的位置,以及它的时间戳。
事件处理代码评估这些
属性以确定如何对事件响应

In iPhone OS, a UITouch object represents a touch, and a UIEvent object represents an event. An event object contains all touch objects for the current multi-touch sequence and can provide touch objects specific to a view or window (see Figure 3-2). A touch object is persistent for a given finger during a sequence, and UIKit mutates it as it tracks the finger throughout it. The touch attributes that change are the phase of the touch, its location in a view, its previous location, and its timestamp. Event-handling code evaluates these attributes to determine how to respond to the event.

这篇关于iPhone:跟踪/识别个人触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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