为什么UIViewController触摸Began,touchesMoved& touchesEnded仅在两次触摸中的第一次开始,移动或结束时被调用? [英] Why are UIViewController touchesBegan, touchesMoved & touchesEnded only being called when the first of two touches begins, moves or ends?

查看:1809
本文介绍了为什么UIViewController触摸Began,touchesMoved& touchesEnded仅在两次触摸中的第一次开始,移动或结束时被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过UIViewController的touchesBegan / Moved / Ended方法处理多个触摸时遇到问题。我也看到在cocos2d应用程序(使用ccTouchesBegan /移动/结束)相同的行为,所以我认为这个问题可以应用于所有的触摸处理在iOS中。我已经把下面的代码,其次是我看到的结果。



所有的方法都是在UIViewController子类上实现的。

   - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
NSLog @Touches Began);
[self logTouchesFor:event];

[super touchesEnded:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@Touches Moved);
[self logTouchesFor:event];

[super touchesEnded:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@Touches Ended);
[self logTouchesFor:event];

[super touchesEnded:touches withEvent:event];
}

- (void)logTouchesFor:(UIEvent *)event
{
int count = 1;

(UITouch * touch in event.allTouches)
{
CGPoint location = [touch locationInView:self.view];

NSLog(@%d:(%.0f,%.0f),count,location.x,location.y);
count ++;
}
}

现在有趣的结果...



单次触摸按预期工作



假设我用拇指触摸屏幕。我在输出窗口看到touchesBegan已经按预期调用。我移动我的拇指和触摸移动被调用。然后我把我的拇指从屏幕上,并触摸已结束被调用。所有这一切正如预期的,我把它包含在问题作为一个控制案例 - 只是为了清楚我的视图控制器接收触摸事件,我没有错过一个 vc.view.userInteractionEnabled =

第二次触摸不会造成touchsBegan,touchesMoved或touchesEnded被调用 >

这是最有趣的一个。让我们说,我用我的拇指触摸屏幕(touchesBegan被称为),并保持它仍然在屏幕上。然后我用我的食指触​​摸屏幕上的其他地方,同时保持我的拇指在同一个地方。 TouchesBegan不叫。然后,让我们说,我移动我的食指,同时保持我的拇指绝对静止(这可以是棘手但它是可能的)。 TouchesMoved不被调用。然后,我抬起食指离开屏幕。不调用TouchesEnded。最后,我移动我的拇指和touchesMoved被调用。然后我从屏幕上提起我的拇指,并触摸Ended被调用。



只是为了清楚:我设置了 self.view.multipleTouchEnabled =

,提供第一次触摸移动



这次我做的事情与上面的例子非常相似。我用拇指触摸屏幕,然后食指同时保持拇指静止。 TouchesBegan被称为当我的拇指击中屏幕,但不是我的食指。现在我移动我的拇指,触摸被称为。不仅如此,在event.allTouches数组中有两次触摸(是的,第二次触摸是我期望的)。这意味着系统知道我第二次触摸屏幕,但我没有通过触摸处理方法通知我的视图控制器。



如何通知第二次触摸的更改?



我真的希望能够响应位置或状态的更改第二次触摸发生时,而不是当第一次触摸也改变时。很多时候这不会是一个问题,因为很难改变一个触摸而不影响另一个,但我有至少一个情况,它可以是一个问题。我缺少明显的东西吗?有没有人注意到这个行为或有问题?



如果相关,我使用运行iOS 5.1的iPhone 3GS。

解决方案

好吧,我开始痛苦地重建我的项目,一次添加一个文件,我想我有它...



有一个视图的子视图有 userInteractionEnabled == YES 。一旦我设置为 NO ,我的视图控制器开始得到touchesBegan /移动/结束调用每次触摸。大概,第二个触摸被子视图声明,而不是我的视图控制器,但我不知道为什么这只会发生在第二次触摸,而不是第一次。



我还没有想到cocos2d项目发生了什么,但是大概是一个不同的问题,因为没有子视图或节点可以做同样的事情。


I'm having an issue with handling more than one touch through the touchesBegan/Moved/Ended methods of UIViewController. I'm also seeing the same behaviour in a cocos2d app (using ccTouchesBegan/Moved/Ended) so I think this question can be applied to all touch handling in iOS. I've put the code that I'm using below, followed by the results that I'm seeing.

All methods are implemented on a UIViewController subclass.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Touches Began");
    [self logTouchesFor: event];

    [super touchesEnded: touches withEvent: event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Touches Moved");
    [self logTouchesFor: event];

    [super touchesEnded: touches withEvent: event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Touches Ended");
    [self logTouchesFor: event];

    [super touchesEnded: touches withEvent: event];
}

-(void)logTouchesFor:(UIEvent *)event
{
    int count = 1;

    for (UITouch *touch in event.allTouches)
    {
        CGPoint location = [touch locationInView: self.view];

        NSLog(@"%d: (%.0f, %.0f)", count, location.x, location.y);
        count++;
    }
}

Now for the interesting results...

Single Touches Work as Expected

Let's say I touch the screen with my thumb. I see in the output window that touchesBegan has been called as expected. I move my thumb around and touchesMoved gets called. Then I lift my thumb off the screen and touchesEnded gets called. All this is as expected, I'm including it in the question as a control case - just to be clear that my view controller is receiving touch events and I haven't missed a vc.view.userInteractionEnabled = YES anywhere.

The Second Touch Doesn't Cause touchesBegan, touchesMoved or touchesEnded to be called

This is the most interesting one. Let's say I touch the screen with my thumb (touchesBegan is called) and hold it still on the screen. Then I touch somewhere else on the screen with my index finger, whilst keeping my thumb in the same place. TouchesBegan is not called. Then let's say I move my index finger whilst keeping my thumb absolutely still (this can be tricky but it is possible). TouchesMoved is not called. Then, I lift my index finger off the screen. TouchesEnded is not called. Finally, I move my thumb and touchesMoved is called. Then I lift my thumb from the screen and touchesEnded is called.

Just to be clear: I've set self.view.multipleTouchEnabled = YES in my viewDidLoad method.

Information About the Second Touch is Available, Providing the First Touch Moves

This time I do something very similar to the example immediately above. I touch the screen with my thumb, then index finger whilst keeping my thumb still. TouchesBegan is called when my thumb hits the screen, but not my index finger. Now I move my thumb, and touchesMoved is called. Not only that, but there are two touches in the event.allTouches array (and yes, the second touch is where I would expect it to be). This means that the system is aware that I have touched the screen a second time, but I am not being notified through the touch handling methods on my view controller.

How Can I be Notified About Changes to the Second Touch?

I'd really like to be able to respond to changes in the location or state of the second touch as they happen, rather than when the first touch also changes. A lot of the time this won't be an issue because it's very hard to change one touch without impacting on the other, but I have at least one situation where it can be an issue. Am I missing something obvious? Has anyone else noticed this behaviour or had issues with it?

In case it's relevant, I'm using an iPhone 3GS running iOS 5.1.

解决方案

Ok, I started painstakingly rebuilding my project, adding in the files one at a time, and I think I've got it...

There was a subview of the view in question which had userInteractionEnabled == YES. Once I set this to NO, my view controller started getting touchesBegan/Moved/Ended calls for each touch. Presumably, the second touch was being claimed by the subview and not making it to my view controller, but I have no idea why this would only happen with the second touch and not the first.

I haven't figured out what's going on with the cocos2d project yet, but presumably it's a different issue as there are no subviews or nodes that could be doing the same thing.

这篇关于为什么UIViewController触摸Began,touchesMoved& touchesEnded仅在两次触摸中的第一次开始,移动或结束时被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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