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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:32:23+00:00 2026-05-22T23:32:23+00:00

I have a UIView class that I am using to have a CALayer. This

  • 0

I have a UIView class that I am using to have a CALayer. This layer will be used to draw lines, based on touch.

This is how the class is defined:

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self == nil) {
        return nil;
    }

    self.layer.backgroundColor = [UIColor redColor].CGColor;
    self.userInteractionEnabled = YES;
    path = CGPathCreateMutable(); 
    return self;
}

then I have the following lines on touchesBegan, TouchesMoved and touchesEnded…

**touchesBegan**
CGPathMoveToPoint(path, NULL, currentPoint.x, currentPoint.y);
[self.layer setNeedsDisplay];


**touchesMoved**
CGPathAddLineToPoint(path, NULL, currentPoint.x, currentPoint.y);
[self.layer setNeedsDisplay];


**touchesEnded**
CGPathAddLineToPoint(path, NULL, currentPoint.x, currentPoint.y);
[self.layer setNeedsDisplay];

then I have this

-(void)drawInContext:(CGContextRef)context {
    CGContextSetStrokeColorWithColor(context, [[UIColor greenColor] CGColor]);
    CGContextSetLineWidth(context, 3.0);
    CGContextBeginPath(context);
    CGContextAddPath(context, path);
    CGContextStrokePath(context);
}

The touchesBegan/Moved/Ended methods are called but this drawInContext method is never called…

what am I missing???

thanks.

  • 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-22T23:32:24+00:00Added an answer on May 22, 2026 at 11:32 pm

    You are conflating layers and views and using CG api when you could easily use UIKit api’s.

    in your init method do this;

    - (id)initWithFrame:(CGRect)frame {
    
        self = [super initWithFrame:frame];
        if (self == nil) {
            return nil;
        }
    
        self.backgroundColor = [UIColor redColor];
        // YES is the default for UIView, only UIImageView defaults to NO
        //self.userInteractionEnabled = YES;
        [self setPath:[UIBezierPath bezierPath]];
        [[self path] setLineWidth:3.0];
        return self;
    }
    

    In your event processing code;

    **touchesBegan**
    [[self path] moveToPoint:currentPoint];
    [self setNeedsDisplay];
    
    
    **touchesMoved**
    [[self path] addLineToPoint:currentPoint];
    [self setNeedsDisplay];
    
    
    **touchesEnded**
    [[self path] addLineToPoint:currentPoint];
    [self setNeedsDisplay];
    

    Then implement drawRect: like this;

    - (void)drawRect:(CGRect)rect {
            [[UIColor greenColor] setStroke];
            [[self path] stroke];
        }
    

    I typed this in from memory, so it might not compile, it might reformat your harddrive or call invaders from mars to invade your home. Well maybe not that…

    The view is the layer’s delegate so what you have would have worked if you named your drawing method drawLayer:inContext:. But don’t do it that way, do what I showed above. For the most part you should not have to think about layers.

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

Sidebar

Related Questions

I have a UIView that will eventually contain about 2 screens worth of information.
I have a UIView subclass ( CustomView for purposes of this question) that has
I am using CATiledLayer as backing layer for my UIView, which I have put
I have GraphicView class that inherits from UIView. Its initWithFrame method is: @implementation GraphicsView
I'm noticing that UIView animations using the UIView class methods seem to more or
I have this UIView that contains two identical UIImageViews juxtaposed. I am looking to
This is probably a simple question... I have a UIView with a button that
I am new to iphone development.I have created a UIview controller class and added
I have a UIView that has several UIImageViews as subviews. Each of these subviews
I have a UIView subclass that draws itself when -drawRect: is called. It only

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.