Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 22, 2013, 11:34 AM   #1
Carll
macrumors newbie
 
Join Date: Jan 2013
drawing a line between two points

Hello

I'm just trying to connect two CGPoint(s) with a line, I've tried with CGContextRef and CGContextAddLineToPoint but didn't work, basically I don't know exactly how to use the CGPoint (so a struct of x and y) as a set of coords :\

Imagine I have a recognizer that catch the tap gesture and make a CGPoint (with the right coords, I've printed the right coords with NSLog so it works, and I store the next coords of every tap in a NSMutableArray, everytime I tap it prints the full array with the latest tap coords, so I know that works well for now in console).

I only need to tell him to draw a line between a tap and the next one, I'm looking for but can't understand exactly what I need

How can I use the CGPoint as coord to draw a Line?

any advice would be appreciated
Carll is offline   0 Reply With Quote
Old Jan 22, 2013, 11:37 AM   #2
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by Carll View Post
I've tried with CGContextRef and CGContextAddLineToPoint but didn't work...
Didn't work how? What exactly did you try (i.e. provide code)? Did you get any warnings / errors / exceptions from the code? If so, what? Please elaborate.
__________________
dejo is offline   0 Reply With Quote
Old Jan 22, 2013, 12:20 PM   #3
Carll
Thread Starter
macrumors newbie
 
Join Date: Jan 2013
Ok thanks, this is the code in the .m file:

Code:
-(IBAction) myTap: (UITapGestureRecognizer *)recognizer {

CGPoint tapPoint = [recognizer locationOfTouch:0 inView: self.uiView];

NSValue *value = [NSValue valueWithCGPoint:tapPoint];

[_coordsArray addObject:value];

NSValue *firstTap = [self.coordsArray objectAtIndex:0];

// I've tried this to see if it's drawing but nothing appears

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 10.0, 20.0);
CGContextAddLineToPoint(context, 20.0, 40.0);
CGContextStrokePath(context);


// both NSLog and myLabel.text are showing the right informations 
// in the console and label, so the code capture the coords position 
//of the tap gesture and store them well in the NSMutableArray

NSLog (@"%@", _coordsArray);

self.myLabel.text = NSStringFromCGPoint(tapPoint);


}


-(void)viewDidLoad
{
[ super viewDidLoad];

self.coordsArray = [NSMutableArray new];

}
I have no warning, no errors and no crashes, simply didn't draw the line at that coords (10.0, 20.0 and 20.0, 40.0) and obviously I dont know exactly how to tell him to draw a line between my two tap gestures... for now he only know the coords of the tap gestures consecutively

for sure that part in orange is wrong, but don't know how to do it properly and don't know exactly what to see to learn it

i did some edits to show better infos

Last edited by Carll; Jan 22, 2013 at 12:26 PM.
Carll is offline   0 Reply With Quote
Old Jan 22, 2013, 12:38 PM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
You won't have a current graphics context within an event handler. This is not where you do drawing: you do it in drawRect:...
robbieduncan is offline   0 Reply With Quote
Old Jan 22, 2013, 12:48 PM   #5
Carll
Thread Starter
macrumors newbie
 
Join Date: Jan 2013
you mean something like this?

Code:
-(void)drawRect: (CGrect) rect {

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 10.0, 20.0);
CGContextAddLineToPoint(context, 20.0, 40.0);
CGContextStrokePath(context);

}
but I dont need a rect, i need two points (which I have) and a line between them
Carll is offline   0 Reply With Quote
Old Jan 22, 2013, 01:36 PM   #6
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by Carll View Post
...but I dont need a rect...
That's not exactly what drawRect: is for.

From the UIView Class Reference:
Quote:
The View Drawing Cycle
View drawing occurs on an as-needed basis. When a view is first shown, or when all or part of it becomes visible due to layout changes, the system asks the view to draw its contents. For views that contain custom content using UIKit or Core Graphics, the system calls the view’s drawRect: method. Your implementation of this method is responsible for drawing the view’s content into the current graphics context, which is set up by the system automatically prior to calling this method. This creates a static visual representation of your view’s content that can then be displayed on the screen.
__________________
dejo is offline   0 Reply With Quote
Old Jan 22, 2013, 01:51 PM   #7
Carll
Thread Starter
macrumors newbie
 
Join Date: Jan 2013
mmmh ok thanks, so for now I've tried this:

Code:
-(void) drawRect:(CGRect) rect {

CGContextRef context = UIGraphicsGetCuttentContext();
CGContextSaveGState(context);
CGContextSetLineWidth(context, 1.0);
CGContextMoveToPoint(context, startPoint.x, startPoint.y);
CGContextAddLineToPoint(context, tapPoint.x, tapPoint.y);
CGContextStrokePath(context);
CGContextRestoreGState(context);

[_view setNeedsDisplay];


}


-(IBAction) myTap: (UITapGestureRecognizer *)recognizer {

CGPoint tapPoint = [recognizer locationOfTouch:0 inView: self.uiView];

NSValue *value = [NSValue valueWithCGPoint:tapPoint];

[_coordsArray addObject:value];

NSValue *firstTap = [self.coordsArray objectAtIndex:0];


}


-(void)viewDidLoad
{
[ super viewDidLoad];

self.coordsArray = [NSMutableArray new];

}
but usin UIGestrureRecognizer to get the coords by tapping how can I pass the CGPoint to the coords in the green and orange highlights?
Carll is offline   0 Reply With Quote
Old Jan 22, 2013, 01:56 PM   #8
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by Carll View Post
...how can I pass the CGPoint to the coords in the green and orange highlights?
Instance variables?

If you're not sure what those are, step away from the real coding and (re)learn the fundamentals of iOS / Objective-C programming.
__________________
dejo is offline   0 Reply With Quote
Old Jan 22, 2013, 02:53 PM   #9
Carll
Thread Starter
macrumors newbie
 
Join Date: Jan 2013
The best way to learn is doing exercises I think I know what those are but probably I'm not sure how to write it properly in this code, if you want to help me and tell me what I missed (even if is something you consider easy) it would be cool, if not, pity...
Carll is offline   0 Reply With Quote
Old Jan 22, 2013, 02:57 PM   #10
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by Carll View Post
I know what those are but probably I'm not sure how to write it properly in this code
Alright. Explain to me what instance variables are, where they're declared and how they can be used (set and retrieved) in code.
__________________
dejo is offline   0 Reply With Quote
Old Jan 22, 2013, 03:16 PM   #11
Carll
Thread Starter
macrumors newbie
 
Join Date: Jan 2013
Quote:
Originally Posted by dejo View Post
Alright. Explain to me what instance variables are, where they're declared and how they can be used (set and retrieved) in code.
an instance variable is a variable associated with a class of objects, that represent an "element" of informations inside that object, to do a classic example, Car class can have a variable called, I don't know, wheels? car plate? something like that, I imagine a variable like a "box" named with a value inside, that can be an Int, a String, BOOL, an object etc

they're declared in the .h file, how to set them? I think writing type and name, for example int intValue, or string stringValue; how to get them? dunno exactly what u mean, for example in my code I should use that variable and replace the point.x point.y, but those needs to be CGPoint (which are C struct with x and y value inside), that's where I'm missing something

ps. keep in mind I'm not telling you I know everything, of course i don't! that's why I'm here just because I need some help and I'm pretty sure if I get that help i'll learn something useful
Carll is offline   0 Reply With Quote
Old Jan 22, 2013, 05:54 PM   #12
chrono1081
macrumors 604
 
chrono1081's Avatar
 
Join Date: Jan 2008
Location: Isla Nublar
I hate to sound like a debbie downer but the others are right. By skipping you're only hurting yourself and it'll make you a lot more confused later on. There is no set way to do anything in programming, you basically have to apply your knowledge of methodologies to solve problems which is why everyone is telling you to start with the basics.

Try starting with this book: http://www.amazon.com/Objective-C-Programming-Ranch-Guide-Guides/dp/0321706285

Its a short book and you'll be able to go through it pretty fast but it'll get you up to speed fast and get you started in the right direction.

Once you learn the fundamentals then you'll be able to start skipping around.
__________________
Mac Pro (2010): 3.33Ghz Intel Xeon (6 core) - 24 GB RAM - NVidia Quadro k5000
Macbook Air (2010): 2.13 Ghz Intel Core 2 Duo - 4GB RAM
chrono1081 is offline   0 Reply With Quote
Old Jan 22, 2013, 06:09 PM   #13
Carll
Thread Starter
macrumors newbie
 
Join Date: Jan 2013
thank you for the book, im going to read it right now, btw I don't want to skip anything, i can't understand why you don't want to explain me what I missed, I'm sure you can do it in a few lines of text and whatever it is i will understand better what seems to lack in my mind, however ok i'll solve it by myself thanks anyway
Carll is offline   0 Reply With Quote
Old Jan 22, 2013, 06:45 PM   #14
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by Carll View Post
thank you for the book, im going to read it right now, btw I don't want to skip anything, i can't understand why you don't want to explain me what I missed, I'm sure you can do it in a few lines of text and whatever it is i will understand better what seems to lack in my mind, however ok i'll solve it by myself thanks anyway
I am equally sure it will take more than a few lines. This is why books and tutorials exist: to give the necessary background, step by step. Without the background, no explanation is ever just a few lines.

One of the problems inexperienced programmers have is they underestimate things. They underestimate what fundamentals are needed, the difficulty of doing certain things, the different ways that things can be done, the amount of time that things will take, the importance of having a clear and logical design, the value of learning the debugger, etc.

Even experienced programmers still underestimate things, such as difficulty, time, etc.
chown33 is offline   0 Reply With Quote
Old Jan 22, 2013, 08:01 PM   #15
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by Carll View Post
i can't understand why you don't want to explain me what I missed, I'm sure you can do it in a few lines of text and whatever it is i will understand better what seems to lack in my mind, however ok i'll solve it by myself thanks anyway
You need to use setNeedsDisplay after your touch event.

You don't call drawRect directly - you just call setNeedsDisplay which tells the device, "Hey, when you have a moment, redraw the screen (or portion of the screen,) would you?" SetNeedsDisplay is set to false right after drawRect is called. SetNeedsDisplay is automatically set to true right when your view is first created - if you'd like to do subsequent drawing after that, you need to manually set setNeedsDisplay.
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)
ArtOfWarfare is offline   0 Reply With Quote
Old Jan 22, 2013, 08:22 PM   #16
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by Carll View Post
they're declared in the .h file, how to set them? I think writing type and name, for example int intValue, or string stringValue; how to get them? dunno exactly what u mean, for example in my code I should use that variable and replace the point.x point.y, but those needs to be CGPoint (which are C struct with x and y value inside), that's where I'm missing something
This paragraph indicates to me that you do not understand instance variables. Or the issue may be that you are not describing it properly. I'm guessing English is not your first language. Is that the case? If so, there may be things that are being "lost in translation".

Anyways, you say:
Quote:
how to set them? I think writing type and name, for example int intValue, or string stringValue;
That is how you declare instance variables (as long as those declarations are in the correct place), but not how you set them. If you use that approach to set them, as in:
Code:
CGPoint tapPoint = ... ;
what you are actually doing is creating a local variable that will hide the instance variable (and you'll get a warning in Xcode). Does that make sense?
__________________
dejo is offline   0 Reply With Quote
Old Jan 22, 2013, 09:30 PM   #17
chrono1081
macrumors 604
 
chrono1081's Avatar
 
Join Date: Jan 2008
Location: Isla Nublar
Quote:
Originally Posted by Carll View Post
thank you for the book, im going to read it right now, btw I don't want to skip anything, i can't understand why you don't want to explain me what I missed, I'm sure you can do it in a few lines of text and whatever it is i will understand better what seems to lack in my mind, however ok i'll solve it by myself thanks anyway
Its not that we don't want to explain things, its just that explaining is going to take longer than you think since there are concepts that you need to understand first.

I don't want you to get discouraged which is why I recommended that book. It'll get you where you want to be really fast and with a good understanding which is the most crucial thing.

With programming it basically works like this:

1. Learn basic and advanced concepts.
2. Learn how to use the documentation (VERY important).
3. Learn about API's, while referencing the documentation.
4. Code some basic apps.
5. Practice and get better!

Without those fundamentals you'll never get far. Here are some resources you will want to use after you finish with that first book I mentioned:

Programming in Objective-C

You don't necessarily have to go through all of it, just go over the parts the first book I mentioned doesn't cover, and keep it close as a reference.

Then move on to this book:

iOS Programming Guide

It looks like a lot of reading but if you practice a little each day you'll be up and programming in no time. Make sure to keep honing your skills and take a break when you feel like you're getting bored with it.

Also, very important, make sure to do all exercises no matter how simple they seem! This will help you memorize the syntax and get a lot better a lot faster.
__________________
Mac Pro (2010): 3.33Ghz Intel Xeon (6 core) - 24 GB RAM - NVidia Quadro k5000
Macbook Air (2010): 2.13 Ghz Intel Core 2 Duo - 4GB RAM
chrono1081 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Carrier: AT&T - Upgraded 1 Line to Put 5s on Other Line Activation Issue segrobja iPhone 20 Sep 25, 2013 11:23 PM
Long commands overwrite existing line, instead of going to new line altjx OS X 10.8 Mountain Lion 2 Aug 18, 2013 10:50 AM
pre-ordered through AT&T for one line, can I activate it to a different line? robdam1001 iPhone 0 Sep 28, 2012 02:01 PM
HELP! If I added a line to get the iPhone 5, how do switch it to my primary line? Big.Mac.Daddy iPhone 1 Sep 21, 2012 01:40 PM

Forum Jump

All times are GMT -5. The time now is 01:01 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC