New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
iOS Development Discussion on developing apps for the iOS platform.

Coordinate Dependent If Statements


Post Reply New Thread Subscribe

 
Thread Tools
ad407

 
Member Since: Apr 20, 2014
Posts: 4
ad407 is on a distinguished road

ad407 is offline
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!
QUOTE Thanks
chas_m

 
chas_m's Avatar
 
Member Since: Jan 22, 2010
Location: Victoria, BC
Posts: 16,617
chas_m has a reputation beyond reputechas_m has a reputation beyond reputechas_m has a reputation beyond reputechas_m has a reputation beyond reputechas_m has a reputation beyond reputechas_m has a reputation beyond reputechas_m has a reputation beyond reputechas_m has a reputation beyond reputechas_m has a reputation beyond reputechas_m has a reputation beyond reputechas_m has a reputation beyond repute
Mac Specs: 2012 MBP, Black speakers, Black Benq second monitor, black(ish) iPhone 5s, Black 2012 iPad, etc.

chas_m is offline
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.
QUOTE Thanks
Mark FX

 
Mark FX's Avatar
 
Member Since: Aug 13, 2011
Location: West Sussex, UK
Posts: 168
Mark FX has a spectacular aura about

Mark FX is offline
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
QUOTE Thanks
ad407

 
Member Since: Apr 20, 2014
Posts: 4
ad407 is on a distinguished road

ad407 is offline
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
QUOTE Thanks
Raz0rEdge

 
Raz0rEdge's Avatar
 
Member Since: Jul 17, 2009
Location: MA
Posts: 7,491
Raz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant future
Mac Specs: 27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3

Raz0rEdge is offline
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.
QUOTE Thanks
ad407

 
Member Since: Apr 20, 2014
Posts: 4
ad407 is on a distinguished road

ad407 is offline
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!
QUOTE Thanks
Raz0rEdge

 
Raz0rEdge's Avatar
 
Member Since: Jul 17, 2009
Location: MA
Posts: 7,491
Raz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant future
Mac Specs: 27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3

Raz0rEdge is offline
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.
QUOTE Thanks
ad407

 
Member Since: Apr 20, 2014
Posts: 4
ad407 is on a distinguished road

ad407 is offline
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
QUOTE Thanks
Slydude

 
Slydude's Avatar
 
Member Since: Nov 15, 2009
Location: North Louisiana, USA
Posts: 6,393
Slydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant future
Mac Specs: 2.8 GHz MacBook Pro 10.8,3 8 GB mem, 2.66 GHz Mac Pro - Dead, iPhone 4

Slydude is online now
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
QUOTE Thanks
Raz0rEdge

 
Raz0rEdge's Avatar
 
Member Since: Jul 17, 2009
Location: MA
Posts: 7,491
Raz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant future
Mac Specs: 27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3

Raz0rEdge is offline
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.
QUOTE Thanks
Slydude

 
Slydude's Avatar
 
Member Since: Nov 15, 2009
Location: North Louisiana, USA
Posts: 6,393
Slydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant futureSlydude has a brilliant future
Mac Specs: 2.8 GHz MacBook Pro 10.8,3 8 GB mem, 2.66 GHz Mac Pro - Dead, iPhone 4

Slydude is online now
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
QUOTE Thanks
Mark FX

 
Mark FX's Avatar
 
Member Since: Aug 13, 2011
Location: West Sussex, UK
Posts: 168
Mark FX has a spectacular aura about

Mark FX is offline
@ 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
QUOTE Thanks

Post Reply New Thread Subscribe


« Possible to Design an iOS App Operates Outside the Lock Screen? | virtual class »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

All times are GMT -4. The time now is 08:10 PM.

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

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?