Coordinate Dependent If Statements

Joined
Apr 20, 2014
Messages
4
Reaction score
0
Points
1
I have recently started to use Xcode and I am having some difficulties.

I have set up a swipe gesture to move an image depending on whether you swipe to the right or left. I want to create an IF statement which is dependent on the current x and y coordinates of this UIImageview. I want the image to move to a different place depending on where it is currently located.

I know how to move the image but just cant figure out how to do the IF statement!

Any help would be greatly appreciated! Thanks!
 
C

chas_m

Guest
You'll probably get an answer to your question if you message one of the moderators and ask them to move this thread to the Developer Playground subforum -- this area is for users who have just switched to the Mac and need help with basic questions.
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
What are you trying to check for in your IF conditional statement ?

To get the x and y coordinates of your image view within it's superview, you would retrieve it's frame origin, or center origin like this.

Code:
    CGPoint myImageViewsbottomLetfPoint = myImageView.frame.origin;
    
    CGPoint myImageViewsCenterPoint = [myImageView center];

    NSLog(@"myImageView's bottom left coordinates x = %f y = %f", myImageViewsbottomLetfPoint.x, myImageViewsbottomLetfPoint.y);
    
    NSLog(@"myImageView's center coordinates x = %f y = %f", myImageViewsCenterPoint.x, myImageViewsCenterPoint.y);

then you would check the x and y coordinates for a specific value, if that's what you're trying to achieve.

Be more specific about what you want to know.

Regards Mark
 
OP
A
Joined
Apr 20, 2014
Messages
4
Reaction score
0
Points
1
I am trying to make the IF statement dependent on what the objects x and y co-ordinates are.

Here is part of my code:
Code:
- (void)oneFingerSwipeLeft:(UITapGestureRecognizer *)recognizer {
    // Code to handle swipe left
    
    if (Ball.origin.x = 12, Ball.origin.y = 20){
    
        //If the x co-ordinate of the ball is at 12 and the y co-ordinate is at 20
        
    CGRect frame = Ball.frame;
    frame.origin.x = 74;
    frame.origin.y = 209;
    
    Ball.frame = frame;
        
    Ball.image = [UIImage imageNamed:@"Ball1.png"];
        
    // Then change the position of the ball to the x co-ordinate = 74 and the y co-ordinate = 209
    }

    if (Ball.origin.x = 74, Ball.origin.y = 209){
        
        //If the x co-ordinate of the ball is at 74 and the y co-ordinate is at 209

    CGRect frame = Ball.frame;
    frame.origin.x = 25;
    frame.origin.y = 54;

    Ball.frame = frame;

    Ball.image = [UIImage imageNamed:@"Ball2.png"];
        
        //Then change the position of the ball to the x co-ordinate = 25 and the y co-ordinate = 54
}
}
The part I'm struggling with is making the condition of the IF Statement dependent on the x and y co-ordinates of the "Ball". So how would I say:

IF the x co-ordinate is 5 and the Y co-ordinate is 7, run this piece of code:

....

Or am I going the complete wrong way about this?

Thanks!
 
Last edited by a moderator:

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,734
Reaction score
2,059
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
Your IF expressions are wrong. You are not checking the values, you are assigning them..:)

In programming languages, the following is how it works for the string [If one is equal to two, set name to "Mac"]

Code:
if (one == two) {
    name = "Mac";
}

A single "=" is used to assign, two "==" signs are used to check equality. Inequality is "!=" and so on..
 
OP
A
Joined
Apr 20, 2014
Messages
4
Reaction score
0
Points
1
Ahhhhhh I understand!

so:

if (Panda.origin.x == 12, Panda.origin.y == 20){

is better but still not correct, xcode is still saying this doesnt work?

Thanks!
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,734
Reaction score
2,059
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
The correct syntax is
Code:
if (Panda.origin.x == 12 && Panda.origin.y == 20) {
....
}

If you are new to programming, you might want to pick up a book on Objective-C to help you through these initial programming issues..
 
OP
A
Joined
Apr 20, 2014
Messages
4
Reaction score
0
Points
1
I have looked at several beginners books but I couldn't find anything concerning if statements and co-ordinates.

However, I have solved my problem with thanks to you so thank you! This has been bugging me for ages! Much appreciated!

- Arin
 

Slydude

Well-known member
Staff member
Moderator
Joined
Nov 15, 2009
Messages
17,595
Reaction score
1,071
Points
113
Location
North Louisiana, USA
Your Mac's Specs
M1 MacMini 16 GB - Ventura, iPhone 14 Pro Max, 2015 iMac 16 GB Monterey
If you are new to programming, you might want to pick up a book on Objective-C to help you through these initial programming issues..

I keep promising myself to explore programming more Can you suggest a few good resources? In the past when I have looked the books seem to assume that you know the basics.

I've kludged together a few very simple Applescripts and did a bit of Visual Basic programming for Excel a while back. Strictly stuff for my own use.
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,734
Reaction score
2,059
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
I have looked at several beginners books but I couldn't find anything concerning if statements and co-ordinates.

However, I have solved my problem with thanks to you so thank you! This has been bugging me for ages! Much appreciated!

- Arin

You need to separate the two things. Look at the "if" statement as more like the following psuedo code:
Code:
if (expression is true)
   do some work
else
   do other work

You can also chain a longer block with something like the following:
Code:
if (expression is true)
    do some work
else if (another expression is true)
    do other work
else
    do default work

Now in your case, the expression that I'm talking about here happens to be a check for the coordinates, but it could be anything else as well.

I keep promising myself to explore programming more Can you suggest a few good resources? In the past when I have looked the books seem to assume that you know the basics.

I've kludged together a few very simple Applescripts and did a bit of Visual Basic programming for Excel a while back. Strictly stuff for my own use.

Objective-C brings object oriented programming concepts to the good old C programming language. They differentiate the Objective-C part from traditional C by putting the commands in []..

So before attacking Obj-C and OOP programming, it's first good to get a solid grounding in regular C programming.

The following C Primer (outdated link removed) will help you get started. Once you have that grounding, you can start with a number of Objective-C primers that build on that. A lot of these Obj-C primers assume you know C well enough and only introduce the Obj-C pieces. If you get a proper book on Obj-C, they might introduce C, but at an accelerated pace though.

Check these out:
About Objective-C
http://courseware.codeschool.com.s3.amazonaws.com/try_ios/objective_c_primer.pdf
Cocoa Dev Central: Learn Objective-C
Make School | Mediabook
 

Slydude

Well-known member
Staff member
Moderator
Joined
Nov 15, 2009
Messages
17,595
Reaction score
1,071
Points
113
Location
North Louisiana, USA
Your Mac's Specs
M1 MacMini 16 GB - Ventura, iPhone 14 Pro Max, 2015 iMac 16 GB Monterey
Thanks for the links. Most of the Excel macros I've done was after they restructured the language to be a bit more object oriented. So the basic concept isn't entirely foreign to me. It was more a keep poking at it until it works though. I've found that often times what's missing for me is the basics of the syntax.

The few Applescripts I've put together recently weren't because the descriptions in a program's Applsscript dictionary were good. They were OK. There was little in the way of useful syntax descriptions. I've had better luck from taking apart other scripts and looking at the code.
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top