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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:45:24+00:00 2026-06-10T04:45:24+00:00

What I have: To create this line, I basically have an UIView and I

  • 0

What I have:

enter image description here

To create this line, I basically have an UIView and I do the following:

void setLayerToLineFromAToB(CALayer *layer, CGPoint a, CGPoint b, CGFloat lineWidth)
{
    CGPoint center = { 0.5 * (a.x + b.x), 0.5 * (a.y + b.y) };
    CGFloat length = sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
    CGFloat angle = atan2(a.y - b.y, a.x - b.x);

    layer.position = center;
    layer.bounds = (CGRect) { {0, 0}, { length + lineWidth, lineWidth } };
    layer.transform = CATransform3DMakeRotation(angle, 0, 0, 1);
}

Note: This code was found here on stackoverflow, so if someone can give me the reference to it I would appreciate.

What I want:

enter image description here

Ok so the “only” thing I need is to create this pattern on the UIView. I know I am able to do this using Quartz2D (a simple way to do it can be found here). But I want to do it by manipulating the CALayer and not going to to the draw method. Why? Because of the transformation I am making on my UIView, I am not able to draw correctly using the draw method.

Edit 1:

Just to illustrate my problem:

enter image description here

Normally what you have is UIView and then you basically just draw something in it (in this case a simple line). The solution I found to get rid of the “gray” area, was to instead of drawing something, just transform the UIView itself. It work well, if you want a fully filled line, the problem comes when you want a dashed one.

  • 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-10T04:45:26+00:00Added an answer on June 10, 2026 at 4:45 am

    Note: The code from Prince did really help me out, so I will give him +10 for the tips. But in the end, I add to come with my own code. I will also add some context to it, so it can be useful for future readers


    The final code was like this:

    -(void)updateLine{
    
          // Important, otherwise we will be adding multiple sub layers
          if ([[[self layer] sublayers] objectAtIndex:0])
            {
                self.layer.sublayers = nil;
            }
    
            CAShapeLayer *shapeLayer = [CAShapeLayer layer];
            [shapeLayer setBounds:self.bounds];
            [shapeLayer setPosition:self.center];
            [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
            [shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]];
            [shapeLayer setLineWidth:3.0f];
            [shapeLayer setLineJoin:kCALineJoinRound];
            [shapeLayer setLineDashPattern:
            [NSArray arrayWithObjects:[NSNumber numberWithInt:10],
            [NSNumber numberWithInt:5],nil]];
    
            // Setup the path
            CGMutablePathRef path = CGPathCreateMutable();
            CGPathMoveToPoint(path, NULL, beginPoint.center.x, beginPoint.center.y);
            CGPathAddLineToPoint(path, NULL, endPoint.center.x, endPoint.center.y);
    
            [shapeLayer setPath:path];
            CGPathRelease(path);
    
            [[self layer] addSublayer:shapeLayer];
    }
    

    In my case, the beginPoint and endPoint are movable by the user, by using KVO. So when one of them moves:

    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        if ([keyPath isEqual:@"position"])
        {
            [self updateLine];
        }
    }
    

    I did play a lot with Prince’s code. I tried on the draw: method, which add a thin line between the dashed line (a bit weird…) and I also tried on initWithFrame:. By itself his code, without any modifications, would give me this kind of errors on the console:

    <Error>: CGContextSaveGState: invalid context 0x0
    <Error>: CGContextSetLineWidth: invalid context 0x0
    <Error>: CGContextSetLineJoin: invalid context 0x0
    <Error>: CGContextSetLineCap: invalid context 0x0
    <Error>: CGContextSetMiterLimit: invalid context 0x0
    <Error>: CGContextSetFlatness: invalid context 0x0
    <Error>: CGContextAddPath: invalid context 0x0
    <Error>: CGContextDrawPath: invalid context 0x0
    <Error>: CGContextRestoreGState: invalid context 0x0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a reddot cms component, in which I added this line Also,
I have tried create this table, but nothing I have tried works from FKs.
I have this: Create Proc CrearNuevoImagen @imagen image AS INSERT INTO Imagenes(imagen) VALUES( @imagen
I have create a module which at this point does nothing but exist the
I have create a vcf file that contains contacts by using this code ContentResolver
I have create name range on sheet A so I need to use this
Im create this code to store page view in database, but i have problem
I have a weird problem. I create this window, but its blank until i
In Interface Builder components I have Gradient Button I would like to create this
I've read this book section about git branches. I have create a branch called

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.