Mac Forums

Mac Forums (http://www.mac-forums.com/forums/)
-   iOS Development (http://www.mac-forums.com/forums/ios-development/)
-   -   Coordinate Dependent If Statements (http://www.mac-forums.com/forums/ios-development/310679-coordinate-dependent-if-statements.html)

ad407 04-20-2014 09:31 PM

Coordinate Dependent If Statements
 
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!

chas_m 04-20-2014 11:08 PM

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.

Mark FX 04-21-2014 03:26 PM

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

ad407 04-21-2014 04:05 PM

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!

Raz0rEdge 04-21-2014 05:20 PM

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..

ad407 04-21-2014 05:56 PM

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 04-21-2014 06:41 PM

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..

ad407 04-21-2014 07:28 PM

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 04-21-2014 07:43 PM

Quote:

Originally Posted by Raz0rEdge (Post 1579251)
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 04-21-2014 09:14 PM

Quote:

Originally Posted by ad407 (Post 1579268)
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.

Quote:

Originally Posted by Slydude (Post 1579272)
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 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:
https://developer.apple.com/library/...roduction.html
http://courseware.codeschool.com.s3....e_c_primer.pdf
Cocoa Dev Central: Learn Objective-C
https://www.makegameswith.us/tutoria...tive-c-primer/

Slydude 04-22-2014 12:19 AM

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.

Mark FX 04-22-2014 07:46 AM

@ ad407 RazOrEdge has answered your question, but it seems to me you need to learn the basics of the C language. so I would reccommend this book to anyone wanting to learn the basics of C, and also the basics of Objective-C, it will be an invaluable source whenever you get stuck looking for the correct syntax.

Objective-C Programming: The Big Nerd Ranch Guide (2nd Edition) (Big Nerd Ranch Guides): Aaron Hillegass, Mikey Ward: 9780321942067: Amazon.com: Books

Regards Mark


All times are GMT -4. The time now is 07:21 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.