Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Sharukh7

macrumors newbie
Original poster
Jul 7, 2015
7
0
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:AnyObjectin 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 and write the code for it. Am I doing this right? I will deeply appreciate any help.
 
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:AnyObjectin 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 and write the code for it. Am I doing this right? I will deeply appreciate any help.
Learning programming languages seemed easy for me then trying to implement it in making programs. You might know what a hammer and socket set are but how do you use that to rebuild an engine. Objective C is massive and you can never know everything, but as you keep trying you learn more and more.

I would look at the UITouch Class and the method called locationInView. This Method should return a CGPoint. A CGPoint is a simple structure that contains 2 variables of type CGFloat for x and y coordinates. By knowing where the X and Y are from a touch should help you know how to pick left and right sides.

I was always thankful here when people didn't give me the whole answer, but pushed me in the right direction so I would discover it myslef. This should help, if I understood your question correctly.

Also, always post your code with code tags so it is easier for people to help you.
 
Last edited:
This definitely helps. I'm really struggling right now, but I definitely like the fact you didn't tell me the exact code. It's always better to figure it out yourself because that helps you learn better.

Thank You
 
So I have been looking into the UITouch Class and I'm still not able to figure out how to get to know where the touch is happening on the screen; left half or right half. Here is what I tried:

func locationInView(_view: UIView?) -> CGPoint {

let leftside = CGPoint(x: size.width / 2, y: size.height)
return leftisde
}

Now how would I say if touch is less than leftside, apply certain amount of impulse? I have been trying to say if UITouch < leftside or leftside < size.width /2 in touchesbegan method, but i keep getting errors.
 
Hint: Click on the "+" on the menu bar above and select "</>code". Then type or paste in your source code. It will end up looking like this:

Code:
func locationInView(_view: UIView?) -> CGPoint {

                let leftside = CGPoint(x: size.width / 2, y: size.height)
                return leftisde
}

This allows us to read the code in a better format, and it'll keep people from reminding you.

As far as your question goes, I'd run thru some of the sample code that Apple and many others provide. Inside Apple's documentation, they usually have links to the sample projects to see how things work.
 
So I have been looking into the UITouch Class and I'm still not able to figure out how to get to know where the touch is happening on the screen; left half or right half. Here is what I tried:

func locationInView(_view: UIView?) -> CGPoint {

let leftside = CGPoint(x: size.width / 2, y: size.height)
return leftisde
}

Now how would I say if touch is less than leftside, apply certain amount of impulse? I have been trying to say if UITouch < leftside or leftside < size.width /2 in touchesbegan method, but i keep getting errors.

Follow what KarlJay said from here on out. It makes it harder for people to help you if your code is not formatted correctly. One thing that I was taught in my programming class and from this forum is to break things down. Also write things in sudo code to help you, which is basically a list of the things you need to do to reach your goal.

I would write out the steps that you want to do.

Get total screen width from iphone, or the X axis.
Divided that X by 2 to find the middle of the screen.
Get user touch point on the screen.
Find the X axis of that point from the touch.
check to see if the x touch point is greater than half of the screen width
if its greater, then move the ball to the right.
else move the ball to the left.

Try to outline the steps first on paper. If a step is to vague, break it down to smaller steps. I am not a big Swift programming guy. That language is going to go through a lot of revisions since it is new. I am an Objective C person.

I think what you are struggling with is what I was back in the day. Your trying things that are more advanced then your skill level. I know it's not fun to hear, but what happens is you start looking all over the internet for code that you can copy paste to make your program work. Even if you get it to work you really don't know why, it just works? The bad thing there is, Yeh, it works! But you have no clue why and would struggle to reproduce it. When you know why things work, you can then solve these problems much easier.

This line of code you wrote looks confusing and wont do much.
Code:
let leftside = CGPoint(x: size.width / 2, y: size.height)
If my goal was to get the touch event I would find the delegate method that deals with touch like
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint cp = [touch locationInView:self];
        CGFloat xPosition = cp.x;


You create a UITouch object from the events. Then the data you are after is the locationInView method from that UITouch object which returns a CGPoint structure called cp in the above example. Then assign the x value from the CGPoint to xPosition.

Again, when you figure this whole thing out you might consider taking a couple steps back in your learning and start with something basic like C or Python to start learning the correct way. If you started with Swift, I think you will get over your head pretty quick and get lost. Then you will just give up thinking that programming is to hard, when you simply started out above your skill level. Just FYI, I spent a year learning C and objective C before I ever tried to make a GUI program like you are. There is so much to know, and it feels like you are trying to build a 2 story house starting on the second level. Get your foundation stuff down then move on.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.