Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

how to draw a simple line?

h2hh2h Posts: 38Registered Users
edited February 2012 in iOS SDK Development
Hi

I wonder if somewhere out there I can find a simple tutorial which explains how to draw a simple line in a view.

I know that there is the 'QuartzDemo' by Apple, but I don't realy get the stuff they do there (Lots of code to draw a line). I think drawing a simple line must be simpler than what is demonstrated in this example :confused:

greetings
Post edited by h2h on
<a href="http://itunes.apple.com/us/app/atomium-pro-periodic-table/id314347624?mt=8" target="_blank">Atomium Pro</a> - Professional Periodic Table - $4.99<br />
<a href="http://itunes.apple.com/us/app/molecular-mass-calculator/id364920984?mt=8" target="_
· ·

Replies

  • rdbellrdbell Posts: 10Registered Users
    edited August 2008
    Check out the Cocoa Drawing Guide and/or the QuartzDemo. :)
    · ·
  • tzurstzurs Posts: 3New Users
    edited August 2008
    Drawing a line is very easy indeed, the "problem" is that drawing a line is an ill defined "request", you need to decide what color you want your line, what width etc.
    Anyhow,
    in your view's drawRect method (assuming you're subclassing a view, that is you have a class that inherits UIView and you are making an instance of this class viewable):
    in drawRect:
    CGContextRef ctx = UIGraphicsGetCurrentContext(); //get the graphics context
    CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1); //there are two relevant color states, "Stroke" -- used in Stroke drawing functions and "Fill" - used in fill drawing functions
    //now we build a "path"
    you can either directly build it on the context or build a path object, here I build it on the context
    CGContextMoveToPoint(ctx, 0, 0);
    //add a line from 0,0 to the point 100,100
    CGContextAddLineToPoint( ctx, 100,100);
    //"stroke" the path
    CGContextStrokePath(ctx);

    check out the various CGContextSet* functions on how to change the line width, cap etc.

    hope this helps,
    -tzurs
    ps.
    thinking about it, it is a few more lines than needed, I usually have a utility function that takes two points and draws a line.
    · ·
  • h2hh2h Posts: 38Registered Users
    edited September 2008
    Thank You a lot!

    There only remains one little problem, how do I integrate the 'ApplicationServices' framework?

    Well I did this as I usually do,

    -> Add -> Existing Framework

    From there I chose the 'ApplicationServices' framework but I keep getting the Error that the framework is not found.

    I suppose that I'm missing a detail here, but I don't see which one...

    greetings
    <a href="http://itunes.apple.com/us/app/atomium-pro-periodic-table/id314347624?mt=8" target="_blank">Atomium Pro</a> - Professional Periodic Table - $4.99<br />
    <a href="http://itunes.apple.com/us/app/molecular-mass-calculator/id364920984?mt=8" target="_
    · ·
  • aceallwaysaceallways Posts: 77Registered Users
    edited December 2008
    why when i use this exact code does it do absolutely nothing?
    · ·
  • scottiphonescottiphone Posts: 802Registered Users
    edited December 2008
    Did you put in your UIView (or subclass) drawRect: method?
    · ·
  • aceallwaysaceallways Posts: 77Registered Users
    edited December 2008
    I think I need some help w/ setting up where to put the code.

    I am not doing it in a new view, rather just a small view object that takes up only a portion of the main view, where should I put the code for this?

    Also how do you call the code to draw and erase different lines when certain actions are called?
    · ·
  • ChetmunChetmun Posts: 22Registered Users
    edited April 2010
    tzurs wrote: »
    Anyhow, in your view's drawRect method (assuming you're subclassing a view, that is you have a class that inherits UIView and you are making an instance of this class viewable):

    Ah, there's the rub. I just have a view controller that is set up in IB with buttons and stuff. It has a view. But do I have to create a separate file in Xcode to subclass UIView just to draw a simple line? Or can I get the context of the view that is part of my view controller and just draw in that with code right in my view controller .m file?
    · ·
  • mesohornymesohorny Posts: 69Banned Members
    edited July 2010
    Chetmun wrote: »
    Ah, there's the rub. I just have a view controller that is set up in IB with buttons and stuff. It has a view. But do I have to create a separate file in Xcode to subclass UIView just to draw a simple line? Or can I get the context of the view that is part of my view controller and just draw in that with code right in my view controller .m file?


    Its simple

    In the Interface Builder Click On the View And Change its class To your View controller class name
    then you need to call the function
    -(void)viewWillAppear:(BOOL)animated
    {
    [self setNeedsDisplay];

    }

    And Specify
    What You want to Draw In View Did Load

    or
    - (void)drawRect:(CGRect)rect {
    printf("hi");
    //get the Graphics Content and Clear It

    CGContextRef ctx= UIGraphicsGetCurrentContext();
    CGContextClearRect(ctx, rect);
    etc....
    }

    If you have any doubts

    follow me on twitter
    safil sunny (safilsunny) on Twitter
    Tour2Kerala| Kerala Tourism | Kerala Tour Packages | Kerala Holiday Packages | Kerala Vacation Packages | Kerala Honeymoon Packages | Kerala Tour Package | Kerala Travel Agents | Kerala Tour Operator | Kerala Travel Packages
    <a href="www.tour2kerala.tk>Kerala Tourism</a>
    · ·
  • RickSDKRickSDK Posts: 1,104Registered Users @ @ @ @
    edited January 2011
    I'm not quite getting this either. I have a standard UIView and I want to draw some lines in it. If I add a "DrawRect" function to it nothing happens.

    If I go to Interface Builder and try to "Click On the View And Change its class To your View controller class name" I'm not sure what that means. When I select the "Class" attribute, it gives me a drop-down of options like UIView, UITabBar etc.

    And no matter what I select or type in that box nothing happens :(

    I hear people talk about sub-classing my main class view and pointing the sub-classed pointer to the class of the upper class and I have no idea what they are talking about. its all jibberish...
    <a href="http://www.pokertrackpro.com/">Poker Track Pro</a>
    · ·
  • DecebalDecebal Posts: 39Registered Users
    edited February 2012
    Lol, I just lol. In Turbo Pascal it's so easy to draw a line and other stuff... yo just:
    Program 1;

    Uses Graph;

    var Gd,Gm:integer;

    begin
    Gd:=VGA;
    Gm:=VGAhi;
    InitGraph(Gd,Gm,'egavga.bgi directory');
    if graphresult = grOk then
    begin
    line(0,0,100,100);
    end
    else
    writeln("ERROR");
    end.
    · ·
  • KijugSoftwareKijugSoftware Posts: 1New Users
    @h2h

    To add a framework, go to the left panel in the main xcode window and click on the tab that displays all of your classes. Click on the project folder and you will be brought to a non-code window. In the white tableview area where the projects and targets are listed, select the target that you wish to add the framework to is it is not already selected. Then go to the fourth tab that says "Build Phases", and you will see four rows that can be opened. Open up the one that says "Link Binary With Libraries", and you will see a list of the frameworks currently in your project/app. Click the + button in the bottom left hand corner to add a new library/framework, and when the selection window comes up, select the framework you want. After that it should appear in the project folder in the tab where your classes are listed.

    This is the way that I have always added my frameworks and it has never failed me. I know that it sounds like a lot, but I'm trying to be as specific as possible.

    Check out our apps on iTunes here
    · ·
Sign In or Register to comment.