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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:03:59+00:00 2026-06-04T20:03:59+00:00

I have a UIView called myView @interface MyView : UIView { UIImage *myPic; NSMutableArray

  • 0

I have a UIView called myView

@interface MyView : UIView {    UIImage *myPic;
        NSMutableArray *myDrawing; }

@end

and i updated this array using touches began, and in the touches moved and touches ended by adding values.

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 //   myDrawing = [[NSMutableArray alloc] initWithCapacity:4];
   [myDrawing addObject:[[NSMutableArray alloc] initWithCapacity:4]]; 
    CGPoint curPoint = [[touches anyObject] locationInView:self]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint curPoint = [[touches anyObject] locationInView:self]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]]; 
    [self setNeedsDisplay];
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint curPoint = [[touches anyObject] locationInView:self]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];
    [self setNeedsDisplay];
}

then i use the draw rect method to update the lines

- (void)drawRect:(CGRect)rect
{
    // Drawing code


    float newHeight;
    float newWidth; 

    if (!myDrawing) {
        myDrawing = [[NSMutableArray alloc] initWithCapacity:0];
    }

    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    if (myPic != NULL) 
    {
        float ratio = myPic.size.height/460; 
        if (myPic.size.width/320 > ratio) 
        {
            ratio = myPic.size.width/320;
        }  

        newHeight = myPic.size.height/ratio;
        newWidth = myPic.size.width/ratio;
        [myPic drawInRect:CGRectMake(0,0,newWidth,newHeight)];
    }

    if ([myDrawing count] > 0) {
        CGContextSetLineWidth(ctx, 3);
        NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"SwatchColor"];
        UIColor *color;
        if (colorData!=nil) {
            // If the data object is valid, unarchive the color we've stored in it.
            color = (UIColor *)[NSKeyedUnarchiver unarchiveObjectWithData:colorData];

        }
        if (color)
        {
            CGContextSetStrokeColorWithColor(ctx, color.CGColor);  
        }
        else
        {
            CGContextSetStrokeColorWithColor(ctx,[UIColor blackColor].CGColor);
        }

        for (int i = 0 ; i < [myDrawing count] ; i++) {
            NSArray *thisArray = [myDrawing objectAtIndex:i];
            if ([thisArray count] > 2) 
            {
                float thisX = [[thisArray objectAtIndex:0] floatValue];
                float thisY = [[thisArray objectAtIndex:1] floatValue];



                CGContextBeginPath(ctx);
                CGContextMoveToPoint(ctx, thisX, thisY);

            for (int j = 2; j < [thisArray count] ; j+=2) 
            { 
                thisX = [[thisArray objectAtIndex:j] floatValue];
                thisY = [[thisArray objectAtIndex:j+1] floatValue];

                CGContextAddLineToPoint(ctx, thisX,thisY);
                //CGContextAddQuadCurveToPoint(ctx, 150, 10, thisX, thisY);
                 // CGContextAddCurveToPoint(ctx , 0, 50, 300, 250, thisX, thisY);
            }
                CGContextStrokePath(ctx);

            }
        }
    }

}

i have a color picker in my code to change the colors and i want to draw lines of different colors each time by selecting colors, but as of now since i am constructing lines and rendering it when i choose initially red and draw a line, and then later select blue and draw a line , now the older line also change to blue instead of red, However i want the red to remain as red, and blue as such can any one help?

  • 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-04T20:04:00+00:00Added an answer on June 4, 2026 at 8:04 pm

    You are always drawing over the drawn lines. Clear myDrawing to make it store only the points that need to be processed, keep already processed points at another array if you need undo/optimized-save functionality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that subclasses UIView called DrawView. This class contains custom drawing
I have a UIView called geopointView. I want to animate slide in this UIview
I have a UIView subclass that draws itself when -drawRect: is called. It only
I have a UIView subclass ( CustomView for purposes of this question) that has
I have a UIView that contains a number of CALayer subclasses. I am using
I have an array (called array in the code below) which contains a number
I have a UIView called activityView , which contains two subviews activityIndicator and cancelOperationsButton
I have a custom UIView called TiledImage which has a single property named tiledImage
I made of subclass of UIView called Bubble using which i want to draw
I have a custom UIView with some buttons as subviews. I am using touchesBegan/Moved/Ended

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.