我已经做了一个应用程序,我想让它兼容多点触摸。我试着四处看看,但答案并不是针对我的。下面是我要做的:
1)我的编码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesMoved:touches withEvent:event];
if (gameState == kGameStatePaused) {(gameState = kGameStateRunning);}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(gameState == kGameStateRunning) {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if(location.x > 400) {
CGPoint yLocation = CGPointMake(playerPaddle.center.x, location.y);
playerPaddle.center = yLocation;
}
if (gameStyle == kGameStyleTwoP) {
if(location.x < 100) {
CGPoint yLocation2 = CGPointMake(computerPaddle.center.x, location.y);
computerPaddle.center = yLocation2;
}
}
}2)我已进入Interface Builder并选中启用多点触控复选框
3)我构建并运行我的应用程序,它可以正常打开,当我去测试多点触控时,我按住"option键“并点击并移动鼠标
4) (我试图让computerPaddle和playerPaddle都移动),但一次只有一个作品
我不得不试着修复它,但我不明白我哪里错了。
任何帮助都是有用的。谢谢。
发布于 2012-05-31 20:39:37
看这行
UITouch *touch = [[event allTouches] anyObject];你只需要触摸一下,而忽略其他的,这就是为什么只有一个东西可以移动的原因。
因此,用for循环替换应该可以解决这个问题
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(gameState == kGameStateRunning) {
for (UITouch *touch in [event allTouches]) {
CGPoint location = [touch locationInView:touch.view];
if(location.x > 400) {
CGPoint yLocation = CGPointMake(playerPaddle.center.x, location.y);
playerPaddle.center = yLocation;
}
if (gameStyle == kGameStyleTwoP) {
if(location.x < 100) {
CGPoint yLocation2 = CGPointMake(computerPaddle.center.x, location.y);
computerPaddle.center = yLocation2;
}
}
}
}发布于 2012-05-31 20:38:16
UIView上有一个名为multipleTouchEnabled的属性,您可以将其设置为YES以启用它(默认为NO)。
此外,您还应该循环处理在touchesMoved中收到的touches集合中的所有触摸。
https://stackoverflow.com/questions/10833718
复制相似问题