我了解多点触控事件的工作原理
- 当手指触摸 View /屏幕时,该 View 将收到带有一组
UITouch
的ccTouchesBegan
。 UITouch
将保留每个手指的位置 (CGPoint
) 及其唯一性。- 如果多个手指同时触摸 View ,将向 View 发送 2 个
UITouch
。 - 有时 View 将收到带有 2 个
UITouch
的ccTouchesBegan
,或者ccTouchesBegan
将针对每个手指触摸一个接一个地调用两次。< - 如果 finger1 正在移动, View 将通过一个
UITouch
接收到ccTouchesMoved
。
我的问题是如何分别用每个手指触摸画线,将 1 或 2 个手指放在屏幕上并为每个手指触摸开始/移动/结束画线?
以下代码在只有单点触摸时有效,但对于多点触摸,由于上述第 3 点和第 4 点,它不会工作。
就是这样
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] > 0) {
// handle multi touch
UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
CGPoint touchLocation1 = [touch1 locationInView: [touch1 view]];
touchLocation1 = [[CCDirector sharedDirector] convertToGL: touchLocation1];
Edge *temEdge1 = (Edge*)[temEdges objectAtIndex:0];
[[temEdge1 end] updateXY:touchLocation1];
[[temEdge1 start] updateXY:touchLocation1];
if ([touches count] > 1) {
UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
CGPoint touchLocation2 = [touch2 locationInView: [touch2 view]];
touchLocation2 = [[CCDirector sharedDirector] convertToGL: touchLocation2];
Edge *temEdge2 = (Edge*)[temEdges objectAtIndex:1];
[[temEdge2 end] updateXY:touchLocation2];
[[temEdge2 start] updateXY:touchLocation2];
}
}
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] > 0) {
// handle multi touch
UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
CGPoint touchLocation1 = [touch1 locationInView: [touch1 view]];
touchLocation1 = [[CCDirector sharedDirector] convertToGL: touchLocation1];
Edge *temEdge1 = (Edge*)[temEdges objectAtIndex:0];
[[temEdge1 end] updateXY:touchLocation1];
if ([touches count] > 1) {
UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
CGPoint touchLocation2 = [touch2 locationInView: [touch2 view]];
touchLocation2 = [[CCDirector sharedDirector] convertToGL: touchLocation2];
Edge *temEdge2 = (Edge*)[temEdges objectAtIndex:1];
[[temEdge2 end] updateXY:touchLocation2];
}
}
}
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] > 0) {
// handle multi touch
UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
CGPoint touchLocation1 = [touch1 locationInView: [touch1 view]];
touchLocation1 = [[CCDirector sharedDirector] convertToGL: touchLocation1];
Edge *temEdge1 = (Edge*)[temEdges objectAtIndex:0];
[[temEdge1 end] updateXY:touchLocation1];
if ([touches count] > 1) {
UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
CGPoint touchLocation2 = [touch2 locationInView: [touch2 view]];
touchLocation2 = [[CCDirector sharedDirector] convertToGL: touchLocation2];
Edge *temEdge2 = (Edge*)[temEdges objectAtIndex:1];
[[temEdge2 end] updateXY:touchLocation2];
}
}
}
-(void)draw
{
[super draw];
glLineWidth(5.f);
ccDrawColor4B(0, 0, 255, 255);
for (Edge *temEdge in temEdges) {
CGPoint start = [[temEdge start] toCCP];
CGPoint end = [[temEdge end] toCCP];
ccDrawLine(start , end);
}
}
最佳答案
您可以尝试将触摸位置数组与不同的触摸相关联(类似于 NSDictionary,其中 UITouches 作为键,NSArrays 点作为值)。然后,您可以使用 ccDrawLine
或任何其他方式(如果您的 draw
方法)绘制这些线。只是不要忘记将这些数组存储在当前触摸结束的地方。
关于Iphone检测每个手指并画线(使用cocos2d/openGL绘制),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13558773/