我制作了一个应用程序,我想让它与多点触控兼容。我试过环顾四周,但答案并不是我特有的。 这是我做的:
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)我构建并运行我的应用程序,它正常打开,当我去测试多点触控时,我按住“选项键”并单击并移动鼠标
4)(我试图让computerPaddle和playerPaddle都移动)但是一次只能做一件作品
我必须尝试修复它,但我无法理解我哪里出错了。
任何帮助都很有用。 THX。
答案 0 :(得分:10)
UIView上有一个名为multipleTouchEnabled
的属性,您可以将其设置为YES以启用它(默认为NO)。
此外,您应该循环处理touches
中收到的touchesMoved
集中的所有触摸。
答案 1 :(得分:2)
看这一行
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;
}
}
}
}