Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Carll

macrumors newbie
Original poster
Jan 10, 2013
16
0
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

macrumors newbie
Original poster
Jan 10, 2013
16
0
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];

[COLOR="SeaGreen"]// I've tried this to see if it's drawing but nothing appears
[/COLOR]
[COLOR="DarkOrange"]CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 10.0, 20.0);
CGContextAddLineToPoint(context, 20.0, 40.0);
CGContextStrokePath(context);[/COLOR]

[COLOR="SeaGreen"]
// 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[/COLOR]

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:

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
You won't have a current graphics context within an event handler. This is not where you do drawing: you do it in drawRect:...
 

Carll

macrumors newbie
Original poster
Jan 10, 2013
16
0
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
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
...but I dont need a rect...

That's not exactly what drawRect: is for.

From the UIView Class Reference:
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.
 

Carll

macrumors newbie
Original poster
Jan 10, 2013
16
0
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, [COLOR="SeaGreen"]startPoint.x[/COLOR], [COLOR="SeaGreen"]startPoint.y[/COLOR]);
CGContextAddLineToPoint(context, [COLOR="DarkOrange"]tapPoint.x[/COLOR], [COLOR="DarkOrange"]tapPoint.y[/COLOR]);
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

macrumors newbie
Original poster
Jan 10, 2013
16
0
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

macrumors newbie
Original poster
Jan 10, 2013
16
0
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 :)
 

chrono1081

macrumors G3
Jan 26, 2008
8,456
4,163
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.
 

Carll

macrumors newbie
Original poster
Jan 10, 2013
16
0
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
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
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.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,561
6,059
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.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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:
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?
 

chrono1081

macrumors G3
Jan 26, 2008
8,456
4,163
Isla Nublar
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.