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.