drawing a line between two points

Discussion in 'iOS Programming' started by Carll, Jan 22, 2013.

  1. macrumors newbie

    Joined:
    Jan 10, 2013
    #1
    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
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    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.
     
  3. Carll, Jan 22, 2013
    Last edited: Jan 22, 2013

    thread starter macrumors newbie

    Joined:
    Jan 10, 2013
    #3
    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 :)
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    You won't have a current graphics context within an event handler. This is not where you do drawing: you do it in drawRect:...
     
  5. thread starter macrumors newbie

    Joined:
    Jan 10, 2013
    #5
    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
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    That's not exactly what drawRect: is for.

    From the UIView Class Reference:
     
  7. thread starter macrumors newbie

    Joined:
    Jan 10, 2013
    #7
    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?
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    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.
     
  9. thread starter macrumors newbie

    Joined:
    Jan 10, 2013
    #9
    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...
     
  10. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Alright. Explain to me what instance variables are, where they're declared and how they can be used (set and retrieved) in code.
     
  11. thread starter macrumors newbie

    Joined:
    Jan 10, 2013
    #11
    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 :)
     
  12. macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #12
    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.
     
  13. thread starter macrumors newbie

    Joined:
    Jan 10, 2013
    #13
    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
     
  14. macrumors 603

    Joined:
    Aug 9, 2009
    #14
    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.
     
  15. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #15
    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.
     
  16. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    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:
    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?
     
  17. macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #17
    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.
     

Share This Page