Results 1 to 12 of 12
  1. #1
    Coordinate Dependent If Statements

    Member Since
    Apr 20, 2014
    Posts
    4
    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!

  2. #2
    Coordinate Dependent If Statements
    chas_m's Avatar
    Member Since
    Jan 22, 2010
    Location
    Victoria, BC
    Posts
    18,965
    Specs:
    Mid-2012 MBP (16GB, 1TB HD), Monoprice 24-inch second monitor, iPhone 5s 32GB, iPad Air 2 64GB
    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.

  3. #3
    Coordinate Dependent If Statements
    Mark FX's Avatar
    Member Since
    Aug 13, 2011
    Location
    West Sussex, UK
    Posts
    180
    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

  4. #4
    Coordinate Dependent If Statements

    Member Since
    Apr 20, 2014
    Posts
    4
    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 Raz0rEdge; 04-21-2014 at 04:17 PM. Reason: CODE'ify segment

  5. #5
    Coordinate Dependent If Statements
    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    8,188
    Specs:
    27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3
    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..
    --
    Regards
    ...Ashwin



    Be sure to read the Community Guidelines | The more information you provide, the better answers you get, remember GIGO.

  6. #6
    Coordinate Dependent If Statements

    Member Since
    Apr 20, 2014
    Posts
    4
    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!

  7. #7
    Coordinate Dependent If Statements
    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    8,188
    Specs:
    27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3
    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..
    --
    Regards
    ...Ashwin



    Be sure to read the Community Guidelines | The more information you provide, the better answers you get, remember GIGO.

  8. #8
    Coordinate Dependent If Statements

    Member Since
    Apr 20, 2014
    Posts
    4
    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

  9. #9
    Coordinate Dependent If Statements
    Slydude's Avatar
    Member Since
    Nov 15, 2009
    Location
    North Louisiana, USA
    Posts
    8,553
    Specs:
    2.8 GHz MacBook Pro 10.10.2, 8 GB mem, iPhone 6+
    Quote Originally Posted by Raz0rEdge View Post
    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.
    Sylvester Roque Former Contributing Editor About This Particular Macintosh

    "Got Time to breathe. You got time for music." Denver Pyle as Briscoe Darling

  10. #10
    Coordinate Dependent If Statements
    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    8,188
    Specs:
    27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3
    Quote Originally Posted by ad407 View Post
    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 View Post
    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/
    --
    Regards
    ...Ashwin



    Be sure to read the Community Guidelines | The more information you provide, the better answers you get, remember GIGO.

  11. #11
    Coordinate Dependent If Statements
    Slydude's Avatar
    Member Since
    Nov 15, 2009
    Location
    North Louisiana, USA
    Posts
    8,553
    Specs:
    2.8 GHz MacBook Pro 10.10.2, 8 GB mem, iPhone 6+
    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.
    Sylvester Roque Former Contributing Editor About This Particular Macintosh

    "Got Time to breathe. You got time for music." Denver Pyle as Briscoe Darling

  12. #12
    Coordinate Dependent If Statements
    Mark FX's Avatar
    Member Since
    Aug 13, 2011
    Location
    West Sussex, UK
    Posts
    180
    @ 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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Unblock statements from my bank
    By paw2 in forum Switcher Hangout
    Replies: 5
    Last Post: 07-21-2012, 01:14 PM
  2. Apple investigating iPhone app to coordinate movie times with other plans
    By OneMoreThing... in forum Apple Rumors and Reports
    Replies: 1
    Last Post: 06-09-2011, 12:46 PM
  3. Replies: 5
    Last Post: 11-04-2009, 03:26 PM
  4. GPS service is dependent on the 3G network???
    By fleurya in forum iPhone Hardware and Accessories
    Replies: 11
    Last Post: 06-18-2008, 10:36 AM
  5. flash IF statements/music player & general Q
    By design2 in forum Web Design and Hosting
    Replies: 0
    Last Post: 03-14-2005, 06:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •