Sharukh
Sharukh

Reputation: 199

How do I detect if there is a tap on right or left side of the iPhone screen?

I'm a fairly new beginner into the iOS world, so forgive me if I leave out some details or if I'm not being clear enough. I have a ball placed on the screen at the bottom and would like to know how to make it go left if the user taps on the left half of iPhone and go right if the user taps on the right half of the iPhone.

The code that I'm trying to make work is this:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    for touch: AnyObject in touches {
        let location = touch.locationOfTouch(<#touchIndex: Int#>, inView: <#UIView?#>)
    }


    ball!.physicsBody!.applyImpulse(CGVectorMake(25.0, 40.0))
}

I know there is code missing, but I can't seem to understand how to approach it. Am I doing this right? I will deeply appreciate your help.

Upvotes: 1

Views: 1444

Answers (2)

Sharukh
Sharukh

Reputation: 199

so I did figure out the code. This is what I did:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    var touch = touches.first as! UITouch
    var point = touch.locationInView(self.view)

    if point.x < size.width / 2 {

        ball!.physicsBody!.applyImpulse(CGVectorMake(-5.0, 10.0))

    }

    else {

        ball!.physicsBody!.applyImpulse(CGVectorMake(5.0, 10.0))

    }

}

Now, I'm coming across another problem, which might not be as complicated as I'm thinking to solve it. So, initially the ball is stationary when the app launches, and it goes in (-5,10) direction if tapped left, and (5,10) if tapped right. The problem is, when I tap right, when the app starts, it goes in (5,10) direction, but it doesn't go in left in the same direction. If I tap on right first when app launches and ball starts moving towards (5,10) direction, I want it to move left in the exact same direction from which it started while moving forward, almost in like a zig zag format. Something like this format /V, if you were to look at that in portrait view, except the line will be the ball going left and right. I hope it makes sense :)

I will keep trying to figure it out and hopefully have it figured out by the time you read it.

Upvotes: 2

Jozey
Jozey

Reputation: 1740

I'm also fairly new so I don't know of a more advanced solution.

What I would do is draw/add a line in the center of the screen. For example, I would use a SKSpriteNode and place it in the center of the screen.

 var middleLine = SKSpriteNode(imageNamed: "CenterLine")
 middleLine.alpha = 0 //you don't want the line to show
 middleLine.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))

This would place the line sprite at the center of the screen. And then, inside the touchesBegan, I would make an if statement concerning if the tap is less than the y coordinate of the center line or greater. If it's less, then the tap would be on the left, if it's greater than the y coordinate, the tap would be on the right.

You would need to make it return the location of the touch where the user tapped in order to compare.

EDIT: To make it easier, you don't even have to add the sprite, just compare the touch x and y to the CGRectGetMidX(self.frame) and CGRectGetMidY(self.frame)

I used this in my game when figuring out if an object was leaving the screen. I compared its x,y coordinates if it was greater than the coordinates inside the frame (or less than).

Upvotes: 0

Related Questions