Search code examples
iosobjective-ccocoa-touchuitouch

Detect touch on the left/right part of the screen


I want to create a game, but I need to detect if the screen was touched on the left or the right side of the screen (in landscape). How can I do that?


Solution

  • I suggest looking at a good tutorial for iOS before posting questions, e.g. iOS Stanford. However, in the mean time, something like this would to the trick:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *myTouch = [touches anyObject];
    
        CGPoint aPoint = [myTouch locationInView:self.view];
    
        if(aPoint.x < 160) { //Left
            NSLog(@"Tapping on the left side of the screen is for communists!");
        else // Right
            NSLog(@"User tapped on the right side! Ohh Yeah!");
    }