Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8673425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:27:20+00:00 2026-06-12T19:27:20+00:00

I began to study Xcode and I can’t understand how to draw a line

  • 0

I began to study Xcode and I can’t understand how to draw a line in a view, when I push button
I have this code

- (void)drawRect:(CGRect)rect
{        
 CGContextRef con = UIGraphicsGetCurrentContext();
 CGContextSetLineWidth(con, 5);
 CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
 CGFloat componen [] = {0.0, 0.0, 1.0, 1.0};
 CGColorRef color = CGColorCreate(space, componen);
 CGContextSetStrokeColorWithColor(con, color);
 CGContextMoveToPoint(con, 0, 0);
 CGContextAddLineToPoint(con, 100, 100);
 CGContextStrokePath(con);
 CGColorSpaceRelease(space);
 CGColorRelease(color);

}

This code draw a line when I start my application, but I wanna to start this code when I push button with my parameters(x1,x2,y1,y2).
I created a function like this

- (void)drawLine
{   
    CGContextRef con = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(con, 5);
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    CGFloat componen [] = {0.0, 0.0, 1.0, 1.0};
    CGColorRef color = CGColorCreate(space, componen);
    CGContextSetStrokeColorWithColor(con, color);
    CGContextMoveToPoint(con, x1, y1);
    CGContextAddLineToPoint(con, x2, y2);
    CGContextStrokePath(con);
    CGColorSpaceRelease(space);
    CGColorRelease(color);

}

But it can’t draw

How to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:27:22+00:00Added an answer on June 12, 2026 at 7:27 pm

    Define your drawRect: so that it decides whether it should draw a line or not:

    - (void)drawRect:(CGRect)rect {
        if (self.drawLine) {
            [self drawLineWithContext: UIGraphicsGetCurrentContext()];
            self.drawLine = NO;
        }
    }
    

    When the button is tapped, have your view controller set your view parameters and tell the system to refresh your view:

    - (IBAction)doDrawing:(id)sender {
        self.drawView.drawLine = YES;
        self.drawView.x1 = 20.0;
        self.drawView.x2 = 200.0;
        self.drawView.y1 = 10.0;
        self.drawView.y2 = 350.0;
        [self.drawView setNeedsDisplay];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently in study of spring framework. I hope i can understand it properly and
I have this problem on my study guide that calls for a function that
First of all I began with the code below in my view controller but
Just began to develop using LINQ, and still can't understand some simple things. So,
I began reading the book Code Complete 2nd edition, but stopped reading when I
I have recently began using C# to invoke powershell scripts, with some success :)
I begin to study qt (I have qt creator, last version), so I read
In my study I faced EJB 2.0 and I don't understand how the bean
Began restructuring my Backbone app referencing this article by Bocoup: http://weblog.bocoup.com/organizing-your-backbone-js-application-with-modules I'm having trouble
I've just began using the powerful event tracking code on one of my projects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.