简体   繁体   中英

how to make multi touch enabled ios app

I have made an app and i want to make it multi touch compatible. i have tried looking around but the answers aren't specific to mine. Here is what i do:

1) My coding:

- (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) I have gone onto Interface Builder and checked the box for multitouch enabled

3) I build and run my app and it opens properly and when i go to test the multitouch i hold "option key" and click and move the mouse

4) (i am trying to make computerPaddle and playerPaddle both move) but only one of the works at a time

I have to tried to fix it but i can't understand where i am going wrong.

Any help is useful. THX.

There is a property named multipleTouchEnabled on the UIView that you can set to YES to enable it (defaults NO).

Also, you should loop to treat all touches in the touches set that you receive in touchesMoved .

look this line

UITouch *touch = [[event allTouches] anyObject];

you only take one touch and ignore rest and thats why only one thing can move

so replace with a for loop should fix the problem

- (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;
        }
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM