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 5962145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:03:52+00:00 2026-05-22T19:03:52+00:00

I’m trying to build some iOS apps while learning and am having some trouble

  • 0

I’m trying to build some iOS apps while learning and am having some trouble understanding the proper way to do this.

What I have currently is a view that is a subclass of UIView. It is clear and I want to use it as a drawing surface. It will sit on top of something else, like tracing paper.

The user should be able to click on one spot then another spot and a line should draw between the 2 points. I’m getting the touch data, I have the points, and I am able to draw stuff from inside of drawRect: initially.

The problem is I’m not sure how to update stuff later. When everything loads up and drawRect: is calle, it will draw a line just fine. But how do I make it draw new stuff or alter stuff that is already drawn based on what the user is doing. I know I need to call setNeedsDisplay, but not how to get the data to the view to draw stuff.

I’ve read a bunch of tutorials/examples and they all stop at “Override drawRect: and draw some stuff… done!”. How do I pass data down in to the view to tell it “hey, redraw this stuff and add this new line”.

Or am I going about this all the wrong way?

EDIT:
I’ll try to explain better the setup I have.

I have a VC. Inside this VC’s view I have a toolbar at the bottom. The rest of the area is taken up by 2 views. One is an image view that holds a reference image. One is the custom view that is clear (tracing paper) that sits at the top. They click a button on the toolbar which turns on a gesturerecognizer. They click on the screen, and I collect the tap data, turn off the gesturerecognizer and HOPEFULLY draw a line. I’ve got it all working except the drawing part.

  • 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-05-22T19:03:53+00:00Added an answer on May 22, 2026 at 7:03 pm

    You are on the right track. Sounds like you need to keep track of the points.

    Here’s some example code.

    LineDrawView.h

    #import <UIKit/UIKit.h>
    @interface LineDrawView : UIView 
    {
        NSMutableArray *lines;
        CGPoint pointA, pointB;
        BOOL activeLine;
    }
    @end
    

    LineDrawView.m

    #import "LineDrawView.h"
    @implementation LineDrawView
    - (id)initWithFrame:(CGRect)frame 
    {
        self = [super initWithFrame:frame];
        if (self) 
        {
            self.backgroundColor = [UIColor blackColor];
            lines = [[NSMutableArray alloc] init];
        }
        return self;
    }
    - (void)dealloc 
    {
        [lines release];
        [super dealloc];
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [super touchesBegan:touches withEvent:event];
        CGPoint point = [[touches anyObject] locationInView:self];
        if ([lines count] == 0) pointA = point;
        else pointB = point;
        activeLine = YES;
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [super touchesMoved:touches withEvent:event];
        pointB = [[touches anyObject] locationInView:self];
        [self setNeedsDisplay];
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [super touchesEnded:touches withEvent:event];
        pointB = [[touches anyObject] locationInView:self];
        [lines addObject:[NSArray arrayWithObjects:[NSValue valueWithCGPoint:pointA], [NSValue valueWithCGPoint:pointB], nil]];
        pointA = pointB;
        pointB = CGPointZero;
        activeLine = NO;
        [self setNeedsDisplay];
    }
    
    - (void)drawRect:(CGRect)rect
    {
        CGContextRef c = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(c, 2);
        CGContextSetLineCap(c, kCGLineCapRound);
        CGContextSetLineJoin(c, kCGLineJoinRound);
        CGContextSetStrokeColorWithColor(c, [UIColor grayColor].CGColor);
        for (NSArray *line in lines)
        {
            CGPoint points[2] = { [[line objectAtIndex:0] CGPointValue], [[line objectAtIndex:1] CGPointValue] };
            CGContextAddLines(c, points, 2);
        }
        CGContextStrokePath(c);
        if (activeLine)
        {
            CGContextSetStrokeColorWithColor(c, [UIColor whiteColor].CGColor);
            CGPoint points2[2] = { pointA, pointB };
            CGContextAddLines(c, points2, 2);
            CGContextStrokePath(c);
        }
    }
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.